Skip to content

Commit 4cc259a

Browse files
authored
Merge pull request #45 from microsoft/dbaeumer/representative-worm-copper
Fix broken python workspace content caching
2 parents afaa12d + c825995 commit 4cc259a

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"publisher": "ms-vscode",
55
"displayName": "Experimental - Python for the Web",
66
"description": "Experimental support for Python in the Web using WebAssemblies",
7-
"version": "0.11.0",
7+
"version": "0.13.0",
88
"author": "Microsoft Corporation",
99
"license": "MIT",
1010
"repository": {

src/common/pythonInstallation.ts

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,55 @@ namespace PythonInstallation {
1717
async function resolveConfiguration(): Promise<{ repository: Uri; root: string | undefined }> {
1818
const path = RAL().path;
1919
let pythonRepository = workspace.getConfiguration('python.wasm').get<string | undefined | null>('runtime', undefined);
20+
let isDefault: boolean = false;
2021
let pythonRoot = undefined;
2122
if (pythonRepository === undefined || pythonRepository === null || pythonRepository.length === 0) {
2223
pythonRepository = defaultPythonRepository;
2324
pythonRoot = defaultPythonRoot;
25+
isDefault = true;
2426
}
2527

26-
// Consider third party file system providers
28+
let pythonRepositoryUri: Uri | undefined = undefined;
2729
try {
28-
let pythonRepositoryUri : Uri | undefined = undefined;
29-
try {
30-
// Uri.parse throws if the URI is invalid
31-
pythonRepositoryUri = Uri.parse(pythonRepository);
32-
} catch (e) {
33-
Tracer.append(`${pythonRepository} is not a valid URI`);
34-
throw e;
35-
}
36-
37-
if (!isGithubUri(pythonRepositoryUri)) {
38-
const binaryLocation = Uri.joinPath(pythonRepositoryUri, 'python.wasm');
39-
try {
40-
// fs.stat throws if file doesn't exist
41-
await workspace.fs.stat(binaryLocation);
42-
Tracer.append(`Using python library from ${pythonRepositoryUri}`);
43-
44-
return { repository: pythonRepositoryUri, root: '/'};
45-
} catch(e) {
46-
Tracer.append(`python.wasm not found in ${binaryLocation}`);
47-
throw e;
48-
}
49-
}
50-
} catch {
51-
Tracer.append(`Falling back to default repository`);
52-
pythonRepository = defaultPythonRepository;
30+
pythonRepositoryUri = Uri.parse(pythonRepository);
31+
} catch (error) {
32+
Tracer.append(`${pythonRepository} is not a valid URI. Falling back to default Python repository ${defaultPythonRepository}`);
33+
}
34+
35+
if (pythonRepositoryUri === undefined) {
36+
pythonRepositoryUri = Uri.parse(defaultPythonRepository);
5337
pythonRoot = defaultPythonRoot;
38+
isDefault = true;
39+
}
40+
41+
// If we point to github.com we need to turn the URI into a virtual one
42+
if (pythonRepositoryUri.authority === 'github.com') {
43+
const uriPath = pythonRepositoryUri.path;
44+
const extname = path.extname(uriPath);
45+
if (extname === '.git') {
46+
pythonRepositoryUri = pythonRepositoryUri.with({ path: uriPath.substring(0, uriPath.length - extname.length) });
47+
}
48+
const api = await RemoteRepositories.getApi();
49+
pythonRepositoryUri = api.getVirtualUri(pythonRepositoryUri.with({ authority: 'github' }));
5450
}
5551

56-
const extname = path.extname(pythonRepository);
57-
if (extname === '.git') {
58-
pythonRepository = pythonRepository.substring(0, pythonRepository.length - extname.length);
52+
// If we are not on the default location make sure we have a python.wasm file
53+
if (!isDefault) {
54+
try {
55+
const binaryLocation = createPythonWasmUri(pythonRepositoryUri, pythonRoot);
56+
await workspace.fs.stat(binaryLocation);
57+
Tracer.append(`Using Python from ${pythonRepositoryUri}`);
58+
} catch (error) {
59+
Tracer.append(`Failed to load Python from ${pythonRepositoryUri}`);
60+
const api = await RemoteRepositories.getApi();
61+
pythonRepositoryUri = api.getVirtualUri(Uri.parse(defaultPythonRepository).with({ authority: 'github' }));
62+
pythonRoot = defaultPythonRoot;
63+
isDefault = true;
64+
Tracer.append(`Falling back to default Python repository ${pythonRepositoryUri}`);
65+
}
5966
}
60-
const api = await RemoteRepositories.getApi();
61-
const vfs = api.getVirtualUri(Uri.parse(pythonRepository)).with({ authority: 'github' });
62-
return { repository: vfs, root: pythonRoot};
67+
68+
return { repository: pythonRepositoryUri, root: pythonRoot};
6369
}
6470

6571
let _configPromise: Promise<{ repository: Uri; root: string | undefined}> | undefined;
@@ -81,12 +87,13 @@ namespace PythonInstallation {
8187
}
8288
});
8389

84-
8590
let _repositoryWatcher: Disposable | undefined;
8691
let preloadToken: number = 0;
8792
async function triggerPreload(): Promise<void> {
8893
const {repository, root} = await getConfig();
89-
if (_repositoryWatcher === undefined) {
94+
const isVSCodeVFS = repository.scheme === 'vscode-vfs';
95+
// We can only preload a repository if we are using a vscode virtual file system.
96+
if (isVSCodeVFS && _repositoryWatcher === undefined) {
9097
const fsWatcher = workspace.createFileSystemWatcher(new RelativePattern(repository, '*'));
9198
_repositoryWatcher = fsWatcher.onDidChange(async (uri) => {
9299
if (uri.toString() === repository.toString()) {
@@ -99,13 +106,13 @@ namespace PythonInstallation {
99106
try {
100107
const token = ++preloadToken;
101108

102-
if (isGithubUri(repository)) {
109+
if (isVSCodeVFS) {
103110
const remoteHubApi = await RemoteRepositories.getApi();
104111
if (remoteHubApi.loadWorkspaceContents !== undefined) {
105112
await remoteHubApi.loadWorkspaceContents(repository);
106113
}
107114
Tracer.append(`Successfully loaded workspace content for repository ${repository.toString()}`);
108-
}
115+
}
109116
const binaryLocation = root !== undefined ? Uri.joinPath(repository, root, 'python.wasm') : Uri.joinPath(repository, 'python.wasm');
110117
try {
111118
const bytes = await workspace.fs.readFile(binaryLocation);
@@ -149,8 +156,8 @@ namespace PythonInstallation {
149156
return wasmBytes;
150157
}
151158

152-
function isGithubUri(uri: Uri): boolean {
153-
return uri.authority === 'github.com';
154-
}
159+
function createPythonWasmUri(repository: Uri, root: string | undefined) {
160+
return root !== undefined ? Uri.joinPath(repository, root, 'python.wasm') : Uri.joinPath(repository, 'python.wasm');
161+
}
155162
}
156163
export default PythonInstallation;

0 commit comments

Comments
 (0)