Skip to content

Commit

Permalink
Prefer open file contents to disk contents (#1110)
Browse files Browse the repository at this point in the history
This seems like what we should be doing anyway, but has the benefit that
when using the language service or the debugger in a multi-file project,
then everything just works even if the files haven't been saved to disk
yet.

Putting up early for review, but in initial testing looked to behave as
expected.
  • Loading branch information
billti authored Feb 3, 2024
1 parent 0e6f17a commit cc082f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
7 changes: 6 additions & 1 deletion vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,12 @@
}
}
}
]
],
"configurationDefaults": {
"[qsharp]": {
"debug.saveBeforeStart": "none"
}
}
},
"scripts": {
"build": "npm run tsc:check && node build.mjs",
Expand Down
37 changes: 27 additions & 10 deletions vscode/src/projectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ async function findManifestDocument(
// vscode-vfs://github%2B7b2276223a312c22726566223a7b2274797065223a332c226964223a22383439227d7d/microsoft/qsharp/samples/shor.qs
const currentDocumentUri = URI.parse(currentDocumentUriString);

// Untitled documents don't have a file location, thus can't have a manifest
if (currentDocumentUri.scheme === "untitled") return null;

// just the parent
// e.g.
// file://home/foo/bar/src
Expand All @@ -64,16 +67,12 @@ async function findManifestDocument(

while (attempts > 0) {
attempts--;
// we abort this check if we are going above the current VS Code
// workspace. If the user is working in a multi-root workspace [1],
// then we do not perform this check. This is because a multi-
// root workspace could contain different roots at different
// levels in each others' path ancestry.
// [1]: https://code.visualstudio.com/docs/editor/workspaces#_multiroot-workspaces

// Make sure that the path doesn't go above one of the open workspaces.
if (
vscode.workspace.workspaceFolders?.length === 1 &&
Utils.resolvePath(vscode.workspace.workspaceFolders[0].uri, "..") ===
uriToQuery
!vscode.workspace.workspaceFolders?.some((root) =>
uriToQuery.toString().startsWith(root.uri.toString()),
)
) {
log.debug("Aborting search for manifest file outside of workspace");
return null;
Expand Down Expand Up @@ -137,6 +136,19 @@ async function readFileUri(
const uri: vscode.Uri = (maybeUri as any).path
? (maybeUri as vscode.Uri)
: vscode.Uri.parse(maybeUri as string);

// If any open documents match this uri, return their contents instead of from disk
const opendoc = vscode.workspace.textDocuments.find(
(opendoc) => opendoc.uri.toString() === uri.toString(),
);

if (opendoc) {
return {
content: opendoc.getText(),
uri,
};
}

try {
return await vscode.workspace.fs.readFile(uri).then((res) => {
return {
Expand All @@ -145,7 +157,12 @@ async function readFileUri(
};
});
} catch (_err) {
// `readFile` returns `err` if the file didn't exist.
// `readFile` should throw the below if the file is not found
if (
!(_err instanceof vscode.FileSystemError && _err.code === "FileNotFound")
) {
log.error("Unexpected error trying to read file", _err);
}
return null;
}
}
Expand Down

0 comments on commit cc082f1

Please sign in to comment.