Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bake: fix the big regressions #15544

Merged
merged 26 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 60 additions & 1 deletion src/bake/BakeGlobalObject.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "BakeGlobalObject.h"
#include "BakeSourceProvider.h"
#include "JSNextTickQueue.h"
#include "JavaScriptCore/GlobalObjectMethodTable.h"
#include "JavaScriptCore/JSInternalPromise.h"
#include "headers-handwritten.h"
#include "JavaScriptCore/JSModuleLoader.h"
#include "JavaScriptCore/Completion.h"
#include "JavaScriptCore/JSSourceCode.h"

extern "C" BunString BakeProdResolve(JSC::JSGlobalObject*, BunString a, BunString b);

Expand Down Expand Up @@ -72,6 +74,58 @@ JSC::Identifier bakeModuleLoaderResolve(JSC::JSGlobalObject* jsGlobal,
return Zig::GlobalObject::moduleLoaderResolve(jsGlobal, loader, key, referrer, origin);
}

static JSC::JSInternalPromise* rejectedInternalPromise(JSC::JSGlobalObject* globalObject, JSC::JSValue value)
{
JSC::VM& vm = globalObject->vm();
JSC::JSInternalPromise* promise = JSC::JSInternalPromise::create(vm, globalObject->internalPromiseStructure());
promise->internalField(JSC::JSPromise::Field::ReactionsOrResult).set(vm, promise, value);
promise->internalField(JSC::JSPromise::Field::Flags).set(vm, promise, JSC::jsNumber(promise->internalField(JSC::JSPromise::Field::Flags).get().asUInt32AsAnyInt() | JSC::JSPromise::isFirstResolvingFunctionCalledFlag | static_cast<unsigned>(JSC::JSPromise::Status::Rejected)));
return promise;
}

static JSC::JSInternalPromise* resolvedInternalPromise(JSC::JSGlobalObject* globalObject, JSC::JSValue value)
{
JSC::VM& vm = globalObject->vm();
JSC::JSInternalPromise* promise = JSC::JSInternalPromise::create(vm, globalObject->internalPromiseStructure());
promise->internalField(JSC::JSPromise::Field::ReactionsOrResult).set(vm, promise, value);
promise->internalField(JSC::JSPromise::Field::Flags).set(vm, promise, JSC::jsNumber(promise->internalField(JSC::JSPromise::Field::Flags).get().asUInt32AsAnyInt() | JSC::JSPromise::isFirstResolvingFunctionCalledFlag | static_cast<unsigned>(JSC::JSPromise::Status::Fulfilled)));
return promise;
}

extern "C" BunString BakeProdLoad(ProductionPerThread* perThreadData, BunString a);

JSC::JSInternalPromise* bakeModuleLoaderFetch(JSC::JSGlobalObject* globalObject,
JSC::JSModuleLoader* loader, JSC::JSValue key,
JSC::JSValue parameters, JSC::JSValue script)
{
Bake::GlobalObject* global = jsCast<Bake::GlobalObject*>(globalObject);
JSC::VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto moduleKey = key.toWTFString(globalObject);
if (UNLIKELY(scope.exception()))
return rejectedInternalPromise(globalObject, scope.exception()->value());

if (moduleKey.startsWith("bake:/"_s)) {
if (LIKELY(global->m_perThreadData)) {
BunString source = BakeProdLoad(global->m_perThreadData, Bun::toString(moduleKey));
if (source.tag != BunStringTag::Dead) {
JSC::SourceOrigin origin = JSC::SourceOrigin(WTF::URL(moduleKey));
JSC::SourceCode sourceCode = JSC::SourceCode(Bake::SourceProvider::create(
source.toWTFString(),
origin,
WTFMove(moduleKey),
WTF::TextPosition(),
JSC::SourceProviderSourceType::Module));
return resolvedInternalPromise(globalObject, JSC::JSSourceCode::create(vm, WTFMove(sourceCode)));
}
return rejectedInternalPromise(globalObject, createTypeError(globalObject, makeString("Bundle does not have \""_s, moduleKey, "\". This is a bug in Bun's bundler."_s)));
}
return rejectedInternalPromise(globalObject, createTypeError(globalObject, "BakeGlobalObject does not have per-thread data configured"_s));
}

return Zig::GlobalObject::moduleLoaderFetch(globalObject, loader, key, parameters, script);
}

#define INHERIT_HOOK_METHOD(name) \
Zig::GlobalObject::s_globalObjectMethodTable.name

Expand All @@ -83,7 +137,7 @@ const JSC::GlobalObjectMethodTable GlobalObject::s_globalObjectMethodTable = {
INHERIT_HOOK_METHOD(shouldInterruptScriptBeforeTimeout),
bakeModuleLoaderImportModule,
bakeModuleLoaderResolve,
INHERIT_HOOK_METHOD(moduleLoaderFetch),
bakeModuleLoaderFetch,
INHERIT_HOOK_METHOD(moduleLoaderCreateImportMetaProperties),
INHERIT_HOOK_METHOD(moduleLoaderEvaluate),
INHERIT_HOOK_METHOD(promiseRejectionTracker),
Expand Down Expand Up @@ -155,4 +209,9 @@ extern "C" GlobalObject* BakeCreateProdGlobal(void* console)
return global;
}

extern "C" void BakeGlobalObject__attachPerThreadData(GlobalObject* global, ProductionPerThread* perThreadData)
{
global->m_perThreadData = perThreadData;
}

}; // namespace Bake
4 changes: 4 additions & 0 deletions src/bake/BakeGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace Bake {

struct ProductionPerThread;

class GlobalObject : public Zig::GlobalObject {
public:
using Base = Zig::GlobalObject;

ProductionPerThread* m_perThreadData;

template<typename, JSC::SubspaceAccess mode> static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
{
if constexpr (mode == JSC::SubspaceAccess::Concurrently)
Expand Down
6 changes: 3 additions & 3 deletions src/bake/BakeSourceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" JSC::EncodedJSValue BakeLoadInitialServerCode(GlobalObject* global, B

String string = "bake://server-runtime.js"_s;
JSC::SourceOrigin origin = JSC::SourceOrigin(WTF::URL(string));
JSC::SourceCode sourceCode = JSC::SourceCode(DevSourceProvider::create(
JSC::SourceCode sourceCode = JSC::SourceCode(SourceProvider::create(
source.toWTFString(),
origin,
WTFMove(string),
Expand Down Expand Up @@ -54,7 +54,7 @@ extern "C" JSC::EncodedJSValue BakeLoadServerHmrPatch(GlobalObject* global, BunS

String string = "bake://server.patch.js"_s;
JSC::SourceOrigin origin = JSC::SourceOrigin(WTF::URL(string));
JSC::SourceCode sourceCode = JSC::SourceCode(DevSourceProvider::create(
JSC::SourceCode sourceCode = JSC::SourceCode(SourceProvider::create(
source.toWTFString(),
origin,
WTFMove(string),
Expand Down Expand Up @@ -117,7 +117,7 @@ extern "C" JSC::EncodedJSValue BakeRegisterProductionChunk(JSC::JSGlobalObject*
String string = virtualPathName.toWTFString();
JSC::JSString* key = JSC::jsString(vm, string);
JSC::SourceOrigin origin = JSC::SourceOrigin(WTF::URL(string));
JSC::SourceCode sourceCode = JSC::SourceCode(DevSourceProvider::create(
JSC::SourceCode sourceCode = JSC::SourceCode(SourceProvider::create(
source.toWTFString(),
origin,
WTFMove(string),
Expand Down
8 changes: 4 additions & 4 deletions src/bake/BakeSourceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

namespace Bake {

class DevSourceProvider final : public JSC::StringSourceProvider {
class SourceProvider final : public JSC::StringSourceProvider {
public:
static Ref<DevSourceProvider> create(
static Ref<SourceProvider> create(
const String& source,
const JSC::SourceOrigin& sourceOrigin,
String&& sourceURL,
const TextPosition& startPosition,
JSC::SourceProviderSourceType sourceType
) {
return adoptRef(*new DevSourceProvider(source, sourceOrigin, WTFMove(sourceURL), startPosition, sourceType));
return adoptRef(*new SourceProvider(source, sourceOrigin, WTFMove(sourceURL), startPosition, sourceType));
}

private:
DevSourceProvider(
SourceProvider(
const String& source,
const JSC::SourceOrigin& sourceOrigin,
String&& sourceURL,
Expand Down
6 changes: 3 additions & 3 deletions src/bake/DevServer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const Options = struct {

// Debugging features
dump_sources: ?[]const u8 = if (Environment.isDebug) ".bake-debug" else null,
dump_state_on_crash: ?bool = false,
dump_state_on_crash: ?bool = null,
verbose_watcher: bool = false,
};

Expand Down Expand Up @@ -904,6 +904,7 @@ fn startAsyncBundle(
.framework = dev.framework,
.client_bundler = &dev.client_bundler,
.ssr_bundler = &dev.ssr_bundler,
.plugins = dev.bundler_options.plugin,
} else @panic("TODO: support non-server components"),
allocator,
.{ .js = dev.vm.eventLoop() },
Expand All @@ -912,7 +913,6 @@ fn startAsyncBundle(
heap,
);
bv2.bun_watcher = dev.bun_watcher;
bv2.plugins = dev.bundler_options.plugin;
bv2.asynchronous = true;

{
Expand Down Expand Up @@ -4352,7 +4352,7 @@ fn dumpStateDueToCrash(dev: *DevServer) !void {
const filepath = std.fmt.bufPrintZ(&filepath_buf, "incremental-graph-crash-dump.{d}.html", .{std.time.timestamp()}) catch "incremental-graph-crash-dump.html";
const file = std.fs.cwd().createFileZ(filepath, .{}) catch |err| {
bun.handleErrorReturnTrace(err, @errorReturnTrace());
Output.warn("Could not open directory for dumping sources: {}", .{err});
Output.warn("Could not open file for dumping incremental graph: {}", .{err});
return;
};
defer file.close();
Expand Down
4 changes: 2 additions & 2 deletions src/bake/bake.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ declare module "bun" {
type GetParamIterator =
| AsyncIterable<Record<string, string>, GetParamsFinalOpts>
| Iterable<Record<string, string>, GetParamsFinalOpts>
| ({ pages: Array<Record<String, String>> } & GetParamsFinalOpts);
| ({ pages: Array<Record<string, string>> } & GetParamsFinalOpts);

type GetParamsFinalOpts = void | null | {
/**
Expand Down Expand Up @@ -516,7 +516,7 @@ declare module "bun" {
* Inject a module into the development server's runtime, to be loaded
* before all other user code.
*/
addPreload(module: string, side: 'client' | 'server'): void;
addPreload(...args: any): void;
}

declare interface OnLoadArgs {
Expand Down
18 changes: 12 additions & 6 deletions src/bake/bake.zig
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ pub const Framework = struct {
} else if (exts_js.isArray()) {
var it_2 = exts_js.arrayIterator(global);
var i_2: usize = 0;
const extensions = try arena.alloc([]const u8, array.getLength(global));
const extensions = try arena.alloc([]const u8, exts_js.getLength(global));
while (it_2.next()) |array_item| : (i_2 += 1) {
const slice = refs.track(try array_item.toSlice2(global, arena));
if (bun.strings.eqlComptime(slice, "*"))
Expand Down Expand Up @@ -600,9 +600,13 @@ pub const Framework = struct {

out.options.framework = framework;

// In development mode, source maps must always be `linked`
// In production, TODO: follow user configuration
out.options.source_map = .linked;
out.options.source_map = switch (mode) {
// Source maps must always be linked, as DevServer special cases the
// linking and part of the generation of these.
.development => .external,
// TODO: follow user configuration
else => .none,
};

out.configureLinker();
try out.configureDefines();
Expand All @@ -615,8 +619,10 @@ pub const Framework = struct {
});

if (mode != .development) {
out.options.entry_naming = "[name]-[hash].[ext]";
out.options.chunk_naming = "chunk-[name]-[hash].[ext]";
// Hide information about the source repository, at the cost of debugging quality.
out.options.entry_naming = "_bun/[hash].[ext]";
out.options.chunk_naming = "_bun/[hash].[ext]";
out.options.asset_naming = "_bun/[hash].[ext]";
}

out.resolver.opts = out.options;
Expand Down
Loading
Loading