Skip to content

Commit 8d68e8e

Browse files
authored
fix(nxls): lazily import @parcel/watcher to avoid issues (#2140)
1 parent e39e8e5 commit 8d68e8e

File tree

5 files changed

+118
-86
lines changed

5 files changed

+118
-86
lines changed

apps/nxls/package.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
"email": "[email protected]"
1515
},
1616
"dependencies": {
17-
"@parcel/watcher": "2.3.0",
18-
"@parcel/watcher-darwin-arm64": "2.3.0",
19-
"@parcel/watcher-darwin-x64": "2.3.0",
20-
"@parcel/watcher-linux-arm-glibc": "2.3.0",
21-
"@parcel/watcher-linux-arm64-glibc": "2.3.0",
22-
"@parcel/watcher-linux-arm64-musl": "2.3.0",
23-
"@parcel/watcher-linux-x64-glibc": "2.3.0",
24-
"@parcel/watcher-linux-x64-musl": "2.3.0",
25-
"@parcel/watcher-win32-arm64": "2.3.0",
26-
"@parcel/watcher-win32-x64": "2.3.0",
27-
"@nx/nx-darwin-arm64": "17.1.0-beta.4",
28-
"@nx/nx-darwin-x64": "17.1.0-beta.4",
29-
"@nx/nx-freebsd-x64": "17.1.0-beta.4",
30-
"@nx/nx-linux-arm-gnueabihf": "17.1.0-beta.4",
31-
"@nx/nx-linux-arm64-gnu": "17.1.0-beta.4",
32-
"@nx/nx-linux-arm64-musl": "17.1.0-beta.4",
33-
"@nx/nx-linux-x64-gnu": "17.1.0-beta.4",
34-
"@nx/nx-linux-x64-musl": "17.1.0-beta.4",
35-
"@nx/nx-win32-arm64-msvc": "17.1.0-beta.4",
36-
"@nx/nx-win32-x64-msvc": "17.1.0-beta.4"
17+
"@parcel/watcher": "2.4.1",
18+
"@parcel/watcher-darwin-arm64": "2.4.1",
19+
"@parcel/watcher-darwin-x64": "2.4.1",
20+
"@parcel/watcher-linux-arm-glibc": "2.4.1",
21+
"@parcel/watcher-linux-arm64-glibc": "2.4.1",
22+
"@parcel/watcher-linux-arm64-musl": "2.4.1",
23+
"@parcel/watcher-linux-x64-glibc": "2.4.1",
24+
"@parcel/watcher-linux-x64-musl": "2.4.1",
25+
"@parcel/watcher-win32-arm64": "2.4.1",
26+
"@parcel/watcher-win32-x64": "2.4.1",
27+
"@nx/nx-darwin-arm64": "19.0.3",
28+
"@nx/nx-darwin-x64": "19.0.3",
29+
"@nx/nx-freebsd-x64": "19.0.3",
30+
"@nx/nx-linux-arm-gnueabihf": "19.0.3",
31+
"@nx/nx-linux-arm64-gnu": "19.0.3",
32+
"@nx/nx-linux-arm64-musl": "19.0.3",
33+
"@nx/nx-linux-x64-gnu": "19.0.3",
34+
"@nx/nx-linux-x64-musl": "19.0.3",
35+
"@nx/nx-win32-arm64-msvc": "19.0.3",
36+
"@nx/nx-win32-x64-msvc": "19.0.3"
3737
}
3838
}

apps/vscode/package.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@
4242
"onStartupFinished"
4343
],
4444
"dependencies": {
45-
"@monodon/typescript-nx-imports-plugin": "0.2.0",
46-
"@parcel/watcher": "2.3.0",
47-
"@parcel/watcher-darwin-arm64": "2.3.0",
48-
"@parcel/watcher-darwin-x64": "2.3.0",
49-
"@parcel/watcher-linux-arm-glibc": "2.3.0",
50-
"@parcel/watcher-linux-arm64-glibc": "2.3.0",
51-
"@parcel/watcher-linux-arm64-musl": "2.3.0",
52-
"@parcel/watcher-linux-x64-glibc": "2.3.0",
53-
"@parcel/watcher-linux-x64-musl": "2.3.0",
54-
"@parcel/watcher-win32-arm64": "2.3.0",
55-
"@parcel/watcher-win32-x64": "2.3.0",
56-
"@nx/nx-darwin-arm64": "17.1.0-beta.4",
57-
"@nx/nx-darwin-x64": "17.1.0-beta.4",
58-
"@nx/nx-freebsd-x64": "17.1.0-beta.4",
59-
"@nx/nx-linux-arm-gnueabihf": "17.1.0-beta.4",
60-
"@nx/nx-linux-arm64-gnu": "17.1.0-beta.4",
61-
"@nx/nx-linux-arm64-musl": "17.1.0-beta.4",
62-
"@nx/nx-linux-x64-gnu": "17.1.0-beta.4",
63-
"@nx/nx-linux-x64-musl": "17.1.0-beta.4",
64-
"@nx/nx-win32-arm64-msvc": "17.1.0-beta.4",
65-
"@nx/nx-win32-x64-msvc": "17.1.0-beta.4"
45+
"@monodon/typescript-nx-imports-plugin": "0.3.0",
46+
"@parcel/watcher": "2.4.1",
47+
"@parcel/watcher-darwin-arm64": "2.4.1",
48+
"@parcel/watcher-darwin-x64": "2.4.1",
49+
"@parcel/watcher-linux-arm-glibc": "2.4.1",
50+
"@parcel/watcher-linux-arm64-glibc": "2.4.1",
51+
"@parcel/watcher-linux-arm64-musl": "2.4.1",
52+
"@parcel/watcher-linux-x64-glibc": "2.4.1",
53+
"@parcel/watcher-linux-x64-musl": "2.4.1",
54+
"@parcel/watcher-win32-arm64": "2.4.1",
55+
"@parcel/watcher-win32-x64": "2.4.1",
56+
"@nx/nx-darwin-arm64": "19.0.3",
57+
"@nx/nx-darwin-x64": "19.0.3",
58+
"@nx/nx-freebsd-x64": "19.0.3",
59+
"@nx/nx-linux-arm-gnueabihf": "19.0.3",
60+
"@nx/nx-linux-arm64-gnu": "19.0.3",
61+
"@nx/nx-linux-arm64-musl": "19.0.3",
62+
"@nx/nx-linux-x64-gnu": "19.0.3",
63+
"@nx/nx-linux-x64-musl": "19.0.3",
64+
"@nx/nx-win32-arm64-msvc": "19.0.3",
65+
"@nx/nx-win32-x64-msvc": "19.0.3"
6666
},
6767
"contributes": {
6868
"typescriptServerPlugins": [
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { lspLogger } from '@nx-console/language-server/utils';
2+
import { importNxPackagePath } from '@nx-console/shared/npm';
3+
import { platform } from 'os';
4+
5+
export class ParcelWatcher {
6+
private subscription: import('@parcel/watcher').AsyncSubscription | undefined;
7+
private stopped = false;
8+
constructor(private workspacePath: string, private callback: () => unknown) {
9+
this.initWatcher();
10+
}
11+
12+
public stop() {
13+
this.subscription?.unsubscribe();
14+
this.stopped = true;
15+
}
16+
17+
private async initWatcher() {
18+
const module = await import('@parcel/watcher');
19+
const watcher = module.default;
20+
21+
this.subscription = await watcher.subscribe(
22+
this.workspacePath,
23+
(err, events) => {
24+
if (this.stopped) {
25+
this.subscription?.unsubscribe();
26+
return;
27+
}
28+
if (err) {
29+
lspLogger.log('Error watching files: ' + err.toString());
30+
} else if (
31+
events.some(
32+
(e) =>
33+
e.path.endsWith('project.json') ||
34+
e.path.endsWith('package.json') ||
35+
e.path.endsWith('nx.json') ||
36+
e.path.endsWith('workspace.json') ||
37+
e.path.endsWith('tsconfig.base.json')
38+
)
39+
) {
40+
lspLogger.log(
41+
`Project configuration changed, ${events.map((e) => e.path)}`
42+
);
43+
this.callback();
44+
}
45+
},
46+
await this.watcherOptions()
47+
);
48+
lspLogger.log('Parcel watcher initialized');
49+
}
50+
51+
private async watcherOptions(): Promise<
52+
import('@parcel/watcher').Options | undefined
53+
> {
54+
const { getIgnoredGlobs } = await importNxPackagePath<
55+
typeof import('nx/src/utils/ignore')
56+
>(this.workspacePath, 'src/utils/ignore', lspLogger);
57+
const ingoredGlobs = getIgnoredGlobs(this.workspacePath).filter(
58+
(glob) => !glob.startsWith('!')
59+
);
60+
const options: import('@parcel/watcher').Options = {
61+
ignore: ingoredGlobs,
62+
};
63+
64+
if (platform() === 'win32') {
65+
options.backend = 'windows';
66+
}
67+
68+
return options;
69+
}
70+
}
Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { lspLogger } from '@nx-console/language-server/utils';
22
import { getNxVersion } from '@nx-console/language-server/workspace';
33
import { debounce } from '@nx-console/shared/utils';
4-
import * as watcher from '@parcel/watcher';
5-
import { platform } from 'os';
64
import { gte } from 'semver';
75
import { DaemonWatcher } from './daemon-watcher';
86
import { NativeWatcher } from './native-watcher';
9-
import { importNxPackagePath } from '@nx-console/shared/npm';
7+
import { ParcelWatcher } from './parcel-watcher';
108

119
let _daemonWatcher: DaemonWatcher | undefined;
1210
let _nativeWatcher: NativeWatcher | undefined;
@@ -45,48 +43,11 @@ export async function languageServerWatcher(
4543
};
4644
}
4745
} else {
48-
const subscription = await watcher.subscribe(
49-
workspacePath,
50-
(err, events) => {
51-
if (err) {
52-
lspLogger.log('Error watching files: ' + err.toString());
53-
} else if (
54-
events.some(
55-
(e) =>
56-
e.path.endsWith('project.json') ||
57-
e.path.endsWith('package.json') ||
58-
e.path.endsWith('nx.json') ||
59-
e.path.endsWith('workspace.json') ||
60-
e.path.endsWith('tsconfig.base.json')
61-
)
62-
) {
63-
lspLogger.log('Project configuration changed');
64-
debouncedCallback();
65-
}
66-
},
67-
await watcherOptions(workspacePath)
68-
);
69-
46+
lspLogger.log('Nx version <16.4.0, using @parcel/watcher');
47+
const parcelWatcher = new ParcelWatcher(workspacePath, debouncedCallback);
7048
return () => {
7149
lspLogger.log('Unregistering file watcher');
72-
subscription.unsubscribe();
50+
parcelWatcher.stop();
7351
};
7452
}
7553
}
76-
77-
async function watcherOptions(
78-
workspacePath: string
79-
): Promise<watcher.Options | undefined> {
80-
const { getIgnoredGlobs } = await importNxPackagePath<
81-
typeof import('nx/src/utils/ignore')
82-
>(workspacePath, 'src/utils/ignore', lspLogger);
83-
const options: watcher.Options = {
84-
ignore: getIgnoredGlobs(workspacePath),
85-
};
86-
87-
if (platform() === 'win32') {
88-
options.backend = 'windows';
89-
}
90-
91-
return options;
92-
}

libs/language-server/watcher/tsconfig.lib.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"module": "commonjs",
4+
"module": "ESNext",
55
"outDir": "../../../dist/out-tsc",
66
"declaration": true,
7-
"types": ["node"]
7+
"types": ["node"],
8+
"esModuleInterop": true
89
},
910
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"],
1011
"include": ["**/*.ts"]

0 commit comments

Comments
 (0)