Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions src/platform/adapters/fs/veryfront/multi-project-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,25 @@ export function getCurrentRequestContext(): RequestContext | null {
return asyncLocalStorage.getStore() ?? null;
}

/**
* Wraps a callback to preserve the current AsyncLocalStorage context.
*
* esbuild communicates with its Go binary via a child process. When esbuild's
* plugin callbacks (onResolve, onLoad) fire, they run in the child process's
* message handler context, which does NOT inherit the AsyncLocalStorage store
* from the original caller. This utility captures the current store and
* re-enters it inside the callback, so that `getAdapter()` can resolve the
* correct per-project adapter.
*/
export function wrapWithCurrentContext<T extends (...args: never[]) => unknown>(fn: T): T {
const store = asyncLocalStorage.getStore();
if (!store) return fn;

return ((...args: Parameters<T>) => {
return asyncLocalStorage.run(store, () => fn(...args));
}) as unknown as T;
Comment on lines +283 to +289

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapWithCurrentContext is typed as T extends (...args: never[]) => unknown, which effectively constrains callbacks to accept no arguments. This will not type-check when wrapping callbacks like esbuild onLoad handlers that require parameters (and can cause callers to need unsafe casts). Update the generic to accept arbitrary parameters (e.g., T extends (...args: unknown[]) => unknown) while preserving Parameters<T>/ReturnType<T> so wrapped functions remain type-safe.

Copilot uses AI. Check for mistakes.
}
Comment on lines +273 to +290

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapWithCurrentContext is a behavior-critical utility (it changes whether request-scoped adapter resolution works under esbuild). There are existing tests for MultiProjectFSAdapter in this directory, but none that validate ALS preservation. Add a focused unit test that creates a wrapped callback inside runWithContext, then invokes it outside the ALS scope and asserts getCurrentRequestContext() (or an adapter method that depends on it) still works.

Copilot uses AI. Check for mistakes.

export function getRequestScopedFile(cacheKey: string): string | undefined {
return asyncLocalStorage.getStore()?.fileCache?.get(cacheKey);
}
Expand Down
88 changes: 50 additions & 38 deletions src/routing/api/module-loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as pathHelper from "#veryfront/compat/path";
import { isDeno, isNode } from "#veryfront/platform/compat/runtime.ts";
import { withSpan } from "#veryfront/observability/tracing/otlp-setup.ts";
import { isCompiledBinary } from "#veryfront/utils";
import { wrapWithCurrentContext } from "#veryfront/platform/adapters/fs/veryfront/multi-project-adapter.ts";

const FILE_EXTENSIONS: string[] = ["", ".ts", ".tsx", ".js", ".jsx", ".mjs"];

Expand Down Expand Up @@ -164,25 +165,28 @@ function createImportMapPlugin(
return { path: absolutePath, namespace: "import-map" };
});

build.onLoad({ filter: /.*/, namespace: "import-map" }, async (args) => {
try {
const { filePath, contents } = await readFileWithExtensions(
adapter,
args.path,
FILE_EXTENSIONS,
);

return {
contents,
loader: getLoaderForFile(filePath),
resolveDir: pathHelper.dirname(filePath),
};
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
logger.error(`[API] Failed to load file via import map: ${args.path}`, error);
return { errors: [{ text: `Failed to load: ${msg}` }] };
}
});
build.onLoad(
{ filter: /.*/, namespace: "import-map" },
wrapWithCurrentContext(async (args) => {
try {
const { filePath, contents } = await readFileWithExtensions(
adapter,
args.path,
FILE_EXTENSIONS,
);

return {
contents,
loader: getLoaderForFile(filePath),
resolveDir: pathHelper.dirname(filePath),
};
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
logger.error(`[API] Failed to load file via import map: ${args.path}`, error);
return { errors: [{ text: `Failed to load: ${msg}` }] };
}
}),
);
},
};
}
Expand All @@ -207,25 +211,33 @@ function createAdapterResolvePlugin(adapter: RuntimeAdapter): Plugin {
return { path: absolutePath, namespace: "vf-adapter" };
});

build.onLoad({ filter: /.*/, namespace: "vf-adapter" }, async (args) => {
try {
const { filePath, contents } = await readFileWithExtensions(
adapter,
args.path,
FILE_EXTENSIONS,
);

return {
contents,
loader: getLoaderForFile(filePath),
resolveDir: pathHelper.dirname(filePath),
};
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
logger.error(`[API] Failed to load via adapter: ${args.path}`, error);
return { errors: [{ text: `Failed to load: ${msg}` }] };
}
});
// Wrap the onLoad callback with wrapWithCurrentContext to preserve the
// AsyncLocalStorage context. esbuild runs in a child process and its plugin
// callbacks fire from the child process message handler, losing the
// AsyncLocalStorage store. Without this, MultiProjectFSAdapter.getAdapter()
// cannot resolve the per-project adapter and all file reads fail silently.
build.onLoad(
{ filter: /.*/, namespace: "vf-adapter" },
wrapWithCurrentContext(async (args) => {
try {
const { filePath, contents } = await readFileWithExtensions(
adapter,
args.path,
FILE_EXTENSIONS,
);

return {
contents,
loader: getLoaderForFile(filePath),
resolveDir: pathHelper.dirname(filePath),
};
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
logger.error(`[API] Failed to load via adapter: ${args.path}`, error);
return { errors: [{ text: `Failed to load: ${msg}` }] };
}
}),
);
},
};
}
Expand Down
Loading