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(linter): do not allow relative import of non-project files #20563

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,42 @@ Violation detected in:
expect(failures[0].message).toEqual(message);
expect(failures[1].message).toEqual(message);
});

it('should error when relatively importing the external resources', () => {
const failures = runRule(
{},
`${process.cwd()}/proj/libs/mylib/src/main.ts`,
`
import '../../../non-project';
import('/tmp/some/path/to/file');
`,
{
nodes: {
mylibName: {
name: 'mylibName',
type: 'lib',
data: {
root: 'libs/mylib',
tags: [],
implicitDependencies: [],
targets: {},
},
},
},
dependencies: {},
},
{
mylibName: [createFile(`libs/mylib/src/main.ts`)],
}
);
expect(failures.length).toEqual(2);
expect(failures[0].message).toEqual(
'External resources cannot be imported using a relative or absolute path'
);
expect(failures[1].message).toEqual(
'External resources cannot be imported using a relative or absolute path'
);
});
});

it('should error on absolute imports into libraries without using the npm scope', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/eslint-plugin/src/rules/enforce-module-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Options = [
];
export type MessageIds =
| 'noRelativeOrAbsoluteImportsAcrossLibraries'
| 'noRelativeOrAbsoluteExternals'
| 'noSelfCircularDependencies'
| 'noCircularDependencies'
| 'noImportsOfApps'
Expand Down Expand Up @@ -159,6 +160,7 @@ export default ESLintUtils.RuleCreator(() => ``)<Options, MessageIds>({
],
messages: {
noRelativeOrAbsoluteImportsAcrossLibraries: `Projects cannot be imported by a relative or absolute path, and must begin with a npm scope`,
noRelativeOrAbsoluteExternals: `External resources cannot be imported using a relative or absolute path`,
noCircularDependencies: `Circular dependency between "{{sourceProjectName}}" and "{{targetProjectName}}" detected: {{path}}\n\nCircular file chain:\n{{filePaths}}`,
noSelfCircularDependencies: `Projects should use relative imports to import from other files within the same project. Use "./path/to/file" instead of import from "{{imp}}"`,
noImportsOfApps: 'Imports of apps are forbidden',
Expand Down Expand Up @@ -345,8 +347,15 @@ export default ESLintUtils.RuleCreator(() => ``)<Options, MessageIds>({
imp
);

// If target is not part of an nx workspace, return.
if (!targetProject) {
// non-project imports cannot use relative or absolute paths
if (isRelativePath(imp) || imp.startsWith('/')) {
context.report({
node,
messageId: 'noRelativeOrAbsoluteExternals',
});
}
// If target is not found (including node internals) we bail early
return;
}

Expand Down