fix(core): Preserve filesystem binding when reading workspace files (no-changelog) (#31667)

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
Jaakko Husso 2026-06-03 18:40:41 +03:00 committed by GitHub
parent 43d32fd28f
commit 001d242af6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 3 deletions

View File

@ -46,6 +46,29 @@ describe('workspace-files', () => {
expect(target.sandbox?.executeCommand).not.toHaveBeenCalled();
});
it('preserves the filesystem as `this` when reading', async () => {
// Mirrors LazyRuntimeFilesystem.readFile, whose body dereferences `this`
// (e.g. `this.getFilesystem()`). A detached call would lose the binding
// and throw "Cannot read properties of undefined".
class ThisDependentFilesystem {
private readonly files = new Map([['/tmp/manifest.json', '{"ok":true}']]);
async readFile(path: string): Promise<string> {
const content = this.files.get(path);
if (content === undefined) throw new Error('missing');
return await Promise.resolve(content);
}
async writeFile(): Promise<void> {
await Promise.resolve();
}
}
const target: WorkspaceFileTarget = { filesystem: new ThisDependentFilesystem() };
await expect(readWorkspaceFile(target, '/tmp/manifest.json')).resolves.toBe('{"ok":true}');
});
it('reads via sandbox commands when no filesystem reader is available', async () => {
const { target } = createWorkspaceTarget(new Map([['/tmp/manifest.json', '{"ok":true}']]));
target.filesystem = {

View File

@ -37,10 +37,10 @@ export async function readWorkspaceFile(
filePath: string,
options?: WorkspaceFileOptions,
): Promise<string | null> {
const readFile = workspace.filesystem?.readFile;
if (readFile) {
const filesystem = workspace.filesystem;
if (filesystem?.readFile) {
try {
return decodeWorkspaceFileContent(await readFile(filePath, { encoding: 'utf-8' }));
return decodeWorkspaceFileContent(await filesystem.readFile(filePath, { encoding: 'utf-8' }));
} catch (error) {
options?.logger?.debug(`${resourceLabel(options)} filesystem read missed`, {
path: filePath,