Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use require instead of import for loading workspace dependency #1165

Merged
merged 1 commit into from
Oct 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as NxWorkspaceFileUtils from '@nrwl/workspace/src/core/file-utils';
import { getOutputChannel } from '@nx-console/server';
import { platform } from 'os';

declare function __non_webpack_require__(importPath: string): any;

/**
* Get the local installed version of @nrwl/workspace
*/
Expand All @@ -14,7 +16,7 @@ export async function getNxWorkspacePackageFileUtils(): Promise<
WorkspaceConfigurationStore.instance.get('nxWorkspaceJsonPath', '')
);

const importPath = join(
let importPath = join(
workspacePath,
'node_modules',
'@nrwl',
Expand All @@ -24,13 +26,21 @@ export async function getNxWorkspacePackageFileUtils(): Promise<
'file-utils.js'
);

return import(
/*webpackIgnore: true*/
platform() === 'win32' ? `file://${importPath}` : importPath
).catch(() => {
getOutputChannel().appendLine(
`Error loading @nrwl/workspace from workspace. Falling back to extension dependency`
);
return NxWorkspaceFileUtils;
return new Promise((res) => {
try {
if (platform() === 'win32') {
importPath = importPath.replace(/\\/g, '/');
}
const imported = __non_webpack_require__(importPath);
return res(imported);
} catch (error) {
getOutputChannel().appendLine(
`
Error loading @nrwl/workspace from workspace. Falling back to extension dependency
Error: ${error}
`
);
return res(NxWorkspaceFileUtils);
}
});
}