Skip to content

Commit 5c7b459

Browse files
committed
Fix path resolving of equally named files
By looking into the previously resolved path first, we make sure to resolve the correct file instead of a file which happens to have the same name and is located in a directory which was added to the node-sass include paths. Fixes #165
1 parent dfc0955 commit 5c7b459

File tree

10 files changed

+49
-8
lines changed

10 files changed

+49
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ Thumbs.db
2929
coverage
3030
dist
3131
node_modules
32+
!test/bugfix/*/files/node_modules
3233
!test/files/node_modules

packages/node-sass-magic-importer/src/functions/build-include-paths.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ describe(`buildIncludePaths()`, () => {
2626
test(`It should return an array with node-sass include paths and include path from previous file.`, () => {
2727
const buildIncludePaths = buildIncludePathsFactory(path);
2828

29-
const nodeSassIncludePaths = `/include/path1${path.delimiter}/include/path2`;
30-
const previouslyResolvedPath = `/include/path3/file.scss`;
29+
const nodeSassIncludePaths = `/include/include-path1${path.delimiter}/include/include-path2`;
30+
const previouslyResolvedPath = `/include/previously-resolved-path/file.scss`;
3131
const expectedResult = [
32-
`/include/path1`,
33-
`/include/path2`,
34-
`/include/path3`,
32+
`/include/previously-resolved-path`,
33+
`/include/include-path1`,
34+
`/include/include-path2`,
3535
];
3636

3737
expect(buildIncludePaths(nodeSassIncludePaths, previouslyResolvedPath))

packages/node-sass-magic-importer/src/functions/build-include-paths.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ export function buildIncludePathsFactory(
55
path: IPath,
66
): IBuildIncludePaths {
77
return (nodeSassIncludePaths: string, previouslyResolvedPath: string) => {
8-
const includePathsSet = new Set(nodeSassIncludePaths.split(path.delimiter));
8+
const includePaths = [];
99

1010
if (path.isAbsolute(previouslyResolvedPath)) {
11-
includePathsSet.add(path.dirname(previouslyResolvedPath));
11+
includePaths.push(path.dirname(previouslyResolvedPath));
1212
}
1313

14-
return [...includePathsSet] as string[];
14+
return [...new Set([...includePaths, ...nodeSassIncludePaths.split(path.delimiter)])] as string[];
1515
};
1616
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.some-file-1 {
2+
content: 'some file 1';
3+
}

test/bugfix/165/files/node_modules/package/_some-file.scss

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/bugfix/165/files/node_modules/package/index.scss

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/bugfix/165/files/node_modules/package/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.some-file-1 {
2+
content: 'some file 1'; }
3+
4+
.some-file-2 {
5+
content: 'some file 2'; }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import 'some-file';
2+
@import '~package';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as fs from 'fs';
2+
import * as sass from 'node-sass';
3+
4+
import * as magicImporter from '../../../packages/node-sass-magic-importer/dist/index';
5+
6+
describe(`magicImporter()`, () => {
7+
test(`It should start trying to resolve paths starting with the path of the parent file.`, () => {
8+
const expectedResult = fs.readFileSync(`test/bugfix/165/files/resolve-from-parent.css`, {
9+
encoding: `utf8`,
10+
});
11+
const result = sass.renderSync({
12+
file: `test/bugfix/165/files/resolve-from-parent.scss`,
13+
importer: magicImporter({
14+
cwd: `${__dirname}/files`,
15+
}),
16+
// This is important for the test case!
17+
includePaths: [`${__dirname}/files`],
18+
}).css.toString();
19+
20+
expect(result).toBe(expectedResult);
21+
});
22+
});

0 commit comments

Comments
 (0)