Skip to content

Commit e371253

Browse files
Fixed race condition with types and fs sync
1 parent d04c6ef commit e371253

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

packages/app/src/app/overmind/effects/vscode/extensionHostWorker/common/fs.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { commonPostMessage } from '@codesandbox/common/lib/utils/global';
1+
import {
2+
commonPostMessage,
3+
getGlobal,
4+
} from '@codesandbox/common/lib/utils/global';
25

36
import { FileSystemConfiguration } from '../../../../../../../../../standalone-packages/codesandbox-browserfs';
47
import { EXTENSIONS_LOCATION } from '../../constants';
@@ -11,7 +14,7 @@ import {
1114
} from '../../sandboxFsSync/utils';
1215
import { getTypeFetcher } from './type-downloader';
1316

14-
const global = self as any;
17+
const global = getGlobal();
1518

1619
export const BROWSER_FS_CONFIG: FileSystemConfiguration = {
1720
fs: 'MountableFileSystem',
@@ -55,7 +58,7 @@ export async function initializeBrowserFS({
5558
syncTypes = false,
5659
extraMounts = {},
5760
} = {}) {
58-
let hasInitialSync = false;
61+
let isInitialSync = true;
5962
return new Promise(resolve => {
6063
const config = { ...BROWSER_FS_CONFIG };
6164
let currentSandboxFs = {};
@@ -91,10 +94,27 @@ export async function initializeBrowserFS({
9194
if (syncSandbox) {
9295
self.addEventListener('message', evt => {
9396
switch (evt.data.$type) {
97+
case 'typings-sync': {
98+
if (isInitialSync) {
99+
commonPostMessage({
100+
$broadcast: true,
101+
$type: 'sync-sandbox',
102+
$data: {},
103+
});
104+
}
105+
break;
106+
}
94107
case 'sandbox-fs': {
95108
currentSandboxFs = evt.data.$data;
96-
if (!hasInitialSync) {
97-
hasInitialSync = true;
109+
if (isInitialSync) {
110+
isInitialSync = false;
111+
global.BrowserFS.BFSRequire(
112+
'fs'
113+
).rename(
114+
'/sandbox/node_modules/@types',
115+
'/sandbox/node_modules/@types',
116+
() => {}
117+
);
98118
resolve();
99119
}
100120
break;
@@ -127,11 +147,19 @@ export async function initializeBrowserFS({
127147
}
128148
});
129149

130-
commonPostMessage({
131-
$broadcast: true,
132-
$type: 'sync-sandbox',
133-
$data: {},
134-
});
150+
if (syncTypes) {
151+
commonPostMessage({
152+
$broadcast: true,
153+
$type: 'sync-types',
154+
$data: {},
155+
});
156+
} else {
157+
commonPostMessage({
158+
$broadcast: true,
159+
$type: 'sync-sandbox',
160+
$data: {},
161+
});
162+
}
135163
} else {
136164
resolve();
137165
}

packages/app/src/app/overmind/effects/vscode/extensionHostWorker/common/type-downloader.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
commonPostMessage,
3-
getGlobal,
4-
} from '@codesandbox/common/lib/utils/global';
1+
import { getGlobal } from '@codesandbox/common/lib/utils/global';
52

63
import {
74
IManager,
@@ -43,11 +40,5 @@ export function getTypeFetcher() {
4340
}
4441
});
4542

46-
commonPostMessage({
47-
$broadcast: true,
48-
$type: 'sync-types',
49-
$data: {},
50-
});
51-
5243
return { options };
5344
}

packages/app/src/app/overmind/effects/vscode/sandboxFsSync/index.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type SandboxFsSyncOptions = {
3939

4040
class SandboxFsSync {
4141
private options: SandboxFsSyncOptions;
42-
private types: any;
42+
private types: any = {};
4343
private typesInfo: Promise<any>;
4444
private initializingWorkers = blocker<void>();
4545
public initialize(options: SandboxFsSyncOptions) {
@@ -173,8 +173,6 @@ class SandboxFsSync {
173173
}
174174

175175
private syncSandbox() {
176-
// eslint-disable-next-line
177-
console.log('## SYNCING SANDBOX WITH WORKERS');
178176
this.send('sandbox-fs', json(this.options.getSandboxFs()));
179177
}
180178

@@ -206,14 +204,20 @@ class SandboxFsSync {
206204
const syncDetails = await this.getDependencyTypingsSyncDetails();
207205

208206
if (syncDetails) {
209-
this.types = await this.getDependencyTypings(
210-
syncDetails.packageJsonContent,
211-
syncDetails.autoInstall
212-
);
213-
// eslint-disable-next-line
214-
console.log('## SYNCING TYPES WITH WORKERS');
215-
this.sendTypes();
207+
try {
208+
this.types = await this.getDependencyTypings(
209+
syncDetails.packageJsonContent,
210+
syncDetails.autoInstall
211+
);
212+
} catch (error) {
213+
// eslint-disable-next-line
214+
console.error(
215+
'Unable to fetch dependency typings, please report it to us!'
216+
);
217+
}
216218
}
219+
220+
this.sendTypes();
217221
}
218222

219223
private async getDependencyTypingsSyncDetails(): Promise<{
@@ -251,6 +255,8 @@ class SandboxFsSync {
251255
);
252256
}
253257
);
258+
} else {
259+
resolve(null);
254260
}
255261
});
256262
} catch (e) {

0 commit comments

Comments
 (0)