Microsoft/monaco-editor

[Bug] Error: Could not find source file

Open

#3,391 opened on Nov 2, 2022

View on GitHub
 (2 comments) (4 reactions) (0 assignees)JavaScript (14,836 stars) (1,283 forks)batch import
bughelp wantedtypescript

Description

Reproducible in vscode.dev or in VS Code Desktop?

  • Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

monaco.editor.create(document.getElementById("container"), {
    model: monaco.editor.createModel(
        "import * as t from 'test';\nt.X.toExponential();", 
        "typescript",
        monaco.Uri.parse("root")
    )
});

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
  ...monaco.languages.typescript.typescriptDefaults.getCompilerOptions(),
  moduleResolution: monaco.languages.typescript.ModuleResolutionKind.Classic
});

const parsed = monaco.Uri.parse("/test.ts?id=123");
monaco.editor.createModel(
    "export const X = 1;", 
    "javascript",
    parsed
);
console.log(parsed)

Reproduction Steps

  1. Paste the code
  2. Open browser debug tool
  3. Press "Run" button
  4. Hover over import statement to make worker work

Actual (Problematic) Behavior

There's an error caused by adding query param (with equal sign or Russian word as example) to URI. The problem is in method getValidSourceFile. This file exists in the list of models, I checked this via program.getSourceFiles(), but its fileName doesn't correspond due to encoding equal sign to %3D, so the method returns undefined and console shows this error:

Uncaught (in promise) Error: Could not find source file: 'file:///test.ts?id%3D123'. at Ve (/vs/language/typescript/tsWorker.js:255:8493) at Object.On [as getSyntacticDiagnostics] (/vs/language/typescript/tsWorker.js:255:13349) at lm.getSyntacticDiagnostics (/vs/language/typescript/tsWorker.js:33955:2559) at S.fmr (workerMain.js:22:33695) at f._handleMessage (workerMain.js:22:17684) at Object.handleMessage (workerMain.js:22:17309) at w._handleRequestMessage (workerMain.js:22:14120) at w._handleMessage (workerMain.js:22:13570) at w.handleMessage (workerMain.js:22:13458) at f.onmessage (workerMain.js:22:17401)

Expected Behavior

File should be returned from the list of model regardless encoding/decoding issues in URI.

Additional Context

No response

Contributor guide