Skip to content

Commit 976cf92

Browse files
authored
fix: Fix how global references are resolved (#4729)
1 parent 00e4086 commit 976cf92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+254
-145
lines changed

.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const config = {
2121
'**/[Ss]amples/**', // cspell:disable-line
2222
'**/[Tt]emp/**',
2323
'**/*.d.ts',
24+
'**/*.d.cts',
25+
'**/*.d.mts',
26+
'**/src/lib*/*.cjs',
2427
'**/*.map',
2528
'**/coverage/**',
2629
'**/cspell-default.config.js',

.github/actions/build-for-integrations/action.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ runs:
9494
cat /home/runner/.pnpm/_logs/*.log
9595
shell: bash
9696

97+
- name: Clean
98+
if: ${{ !steps.step-cache-build.outputs.cache-hit }}
99+
run: pnpm run clean
100+
shell: bash
101+
97102
- name: Build
98103
if: ${{ !steps.step-cache-build.outputs.cache-hit }}
99104
run: pnpm run build

.github/actions/build/action.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ runs:
110110
cat /home/runner/.pnpm/_logs/*.log
111111
shell: bash
112112

113+
- name: Clean
114+
if: ${{ !steps.step-cache-build.outputs.cache-hit }}
115+
run: pnpm run clean
116+
shell: bash
117+
113118
- name: Build
114119
if: ${{ !steps.step-cache-build.outputs.cache-hit }}
115120
run: pnpm run build

.github/workflows/coverage.yml

+2-11
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,9 @@ jobs:
3838
steps:
3939
- uses: actions/checkout@v3
4040

41-
- name: Setup pnpm
42-
uses: pnpm/action-setup@v2.4.0
41+
- name: Install and Build
42+
uses: ./.github/actions/install-build
4343

44-
- name: Use Node.js
45-
uses: actions/setup-node@v3
46-
with:
47-
cache: pnpm
48-
49-
- run: pnpm -v
50-
51-
- run: pnpm i
52-
- run: pnpm run build
5344
- run: pnpm run coverage
5445

5546
- name: Upload coverage Coveralls

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ lcov.info
3636

3737
dist
3838
out
39+
*.d.cts
3940

4041
# And temp files
4142

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ test-fixtures/**
2828
.pnp.js
2929
**/fixtures/with-errors/**
3030
api.d.ts
31+
**/*.d.cts
32+
**/*.cjs

cspell.code-workspace

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
{ "path": "packages/cspell-json-reporter" },
1919
{ "path": "packages/cspell-lib" },
2020
{ "path": "packages/cspell-pipe" },
21+
{ "path": "packages/cspell-resolver" },
2122
{ "path": "packages/cspell-service-bus" },
2223
{ "path": "packages/cspell-strong-weak-map" },
2324
{ "path": "packages/cspell-tools" },

packages/cspell-gitignore/src/GitIgnore.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ describe('GitIgnoreServer', () => {
2626
${__dirname} | ${[packages]} | ${[pkg]}
2727
${__dirname} | ${[pkg, gitRoot]} | ${[pkg]}
2828
${p(samples, 'ignored')} | ${undefined} | ${[gitRoot, pkg, samples]}
29-
${p(pkgCSpellLib)} | ${[pkg]} | ${[gitRoot]}
30-
${p(pkgCSpellLib)} | ${[packages]} | ${[]}
29+
${p(pkgCSpellLib)} | ${[pkg]} | ${[gitRoot, pkgCSpellLib]}
30+
${p(pkgCSpellLib)} | ${[packages]} | ${[pkgCSpellLib]}
3131
`('findGitIgnoreHierarchy $dir $roots', async ({ dir, roots, expected }) => {
3232
const gs = new GitIgnore(roots);
3333
const r = await gs.findGitIgnoreHierarchy(dir);

packages/cspell-gitignore/src/__snapshots__/app.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ exports[`app > app.run [ ' ' ] 1`] = `""`;
2525
2626
exports[`app > app.run [ ' ' ] 2`] = `"Missing files"`;
2727
28-
exports[`app > app.run [ '../node_modules' ] 1`] = `".gitignore:47:node_modules/ ../node_modules"`;
28+
exports[`app > app.run [ '../node_modules' ] 1`] = `".gitignore:48:node_modules/ ../node_modules"`;
2929
3030
exports[`app > app.run [ '../node_modules' ] 2`] = `""`;
3131
@@ -45,7 +45,7 @@ exports[`app > app.run [ 'src/code.ts' ] 1`] = `":: src/code.ts"`;
4545
4646
exports[`app > app.run [ 'src/code.ts' ] 2`] = `""`;
4747
48-
exports[`app > app.run [ 'temp' ] 1`] = `".gitignore:42:temp temp"`;
48+
exports[`app > app.run [ 'temp' ] 1`] = `".gitignore:43:temp temp"`;
4949
5050
exports[`app > app.run [ 'temp' ] 2`] = `""`;
5151

packages/cspell-gitignore/vitest.config.ts packages/cspell-gitignore/vitest.config.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mergeConfig } from 'vite';
22
import { defineConfig } from 'vitest/config';
33

4-
import viteConfig from '../../vitest.config';
4+
import viteConfig from '../../vitest.config.mjs';
55

66
export default mergeConfig(
77
viteConfig,

packages/cspell-lib/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/**/*.cjs

packages/cspell-lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"dependencies": {
6161
"@cspell/cspell-bundled-dicts": "workspace:*",
6262
"@cspell/cspell-pipe": "workspace:*",
63+
"@cspell/cspell-resolver": "workspace:*",
6364
"@cspell/cspell-types": "workspace:*",
6465
"@cspell/strong-weak-map": "workspace:*",
6566
"clear-module": "^4.1.2",
@@ -76,7 +77,6 @@
7677
"gensequence": "^5.0.2",
7778
"import-fresh": "^3.3.0",
7879
"resolve-from": "^5.0.0",
79-
"resolve-global": "^1.0.0",
8080
"vscode-languageserver-textdocument": "^1.0.8",
8181
"vscode-uri": "^3.0.7"
8282
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../../../tsconfig.cjs.json",
3+
"compilerOptions": {
4+
"allowJs": false,
5+
"composite": true,
6+
"tsBuildInfoFile": "../../temp/compile.lib-cjs.tsbuildInfo",
7+
"module": "CommonJS",
8+
"moduleResolution": "node16",
9+
"rootDir": ".",
10+
"outDir": "../../dist/lib-cjs",
11+
"types": ["node"]
12+
},
13+
"files": ["index.cts", "pkg-info.cts", "vscodeUri.cts"]
14+
}
+1-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
{
2-
"extends": "../../../../tsconfig.cjs.json",
3-
"compilerOptions": {
4-
"allowJs": false,
5-
"composite": true,
6-
"tsBuildInfoFile": "../../temp/compile.lib-cjs.tsbuildInfo",
7-
"module": "CommonJS",
8-
"moduleResolution": "node16",
9-
"rootDir": ".",
10-
"outDir": "../../dist/lib-cjs",
11-
"types": ["node"]
12-
},
13-
"include": ["**/*.cts"]
2+
"references": [{ "path": "./tsconfig.cjs.json" }, { "path": "./tsconfig.test.json" }]
143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Note: this configuration is needed to work around a bug in vite / vitest that cannot
3+
convert .cts files to .cjs.
4+
*/
5+
{
6+
"extends": "./tsconfig.cjs.json",
7+
"compilerOptions": {
8+
"declarationMap": false,
9+
"sourceMap": false,
10+
"tsBuildInfoFile": "../../temp/compile.lib-test.tsbuildInfo",
11+
"outDir": ".",
12+
"types": ["node"]
13+
}
14+
}

packages/cspell-lib/src/lib/util/resolveFile.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { resolveGlobal } from '@cspell/cspell-resolver';
12
import * as fs from 'fs';
23
import * as os from 'os';
34
import * as path from 'path';
45
import resolveFrom from 'resolve-from';
5-
import resolveGlobal from 'resolve-global';
66

77
export interface ResolveFileResult {
88
filename: string;
@@ -94,7 +94,7 @@ function tryNodeResolve(filename: string, relativeTo: string): ResolveFileResult
9494
}
9595

9696
function tryResolveGlobal(filename: string): ResolveFileResult {
97-
const r = resolveGlobal.silent(filename);
97+
const r = resolveGlobal(filename);
9898
return { filename: r || filename, relativeTo: undefined, found: !!r };
9999
}
100100

packages/cspell-lib/tsconfig.esm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"types": ["node"]
1212
},
1313
"include": ["src/lib", "src/lib-cjs/vscode-uri.cts"],
14-
"references": [{ "path": "./src/lib-cjs" }, { "path": "./src/test-util" }]
14+
"references": [{ "path": "./src/lib-cjs/tsconfig.cjs.json" }, { "path": "./src/test-util" }]
1515
}

packages/cspell-resolver/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sample .gitignore
2+
3+
dist
4+
temp

packages/cspell-resolver/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Change Log

packages/cspell-resolver/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Jason Dent
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/cspell-resolver/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# CSpell Module Resolver
2+
3+
A library to assist with resolving module and file locations for import / loading.
4+
5+
## Install
6+
7+
```sh
8+
npm install -S @cspell/cspell-resolver
9+
```
10+
11+
<!--- @@inject: ../../static/footer.md --->

packages/cspell-resolver/package.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "@cspell/cspell-resolver",
3+
"publishConfig": {
4+
"access": "public"
5+
},
6+
"version": "7.0.0",
7+
"description": "Library to help resolve module locations.",
8+
"keywords": [
9+
"cspell",
10+
"resolve"
11+
],
12+
"author": "Jason Dent <jason@streetsidesoftware.nl>",
13+
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-resolver#readme",
14+
"license": "MIT",
15+
"type": "commonjs",
16+
"main": "dist/index.js",
17+
"typings": "dist/index.d.ts",
18+
"directories": {
19+
"dist": "dist"
20+
},
21+
"types": "dist/index.d.ts",
22+
"files": [
23+
"dist",
24+
"!**/*.tsbuildInfo",
25+
"!**/__mocks__",
26+
"!**/*.spec.*",
27+
"!**/*.test.*",
28+
"!**/test/**",
29+
"!**/*.map"
30+
],
31+
"scripts": {
32+
"build": "tsc -p .",
33+
"watch": "tsc -p . -w",
34+
"clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
35+
"clean-build": "pnpm run clean && pnpm run build",
36+
"coverage": "vitest run --coverage",
37+
"test-watch": "vitest",
38+
"test": "vitest run"
39+
},
40+
"repository": {
41+
"type": "git",
42+
"url": "git+https://github.com/streetsidesoftware/cspell.git"
43+
},
44+
"bugs": {
45+
"url": "https://github.com/streetsidesoftware/cspell/issues"
46+
},
47+
"engines": {
48+
"node": ">=16"
49+
},
50+
"dependencies": {
51+
"global-dirs": "^3.0.1"
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`index > index methods exist 1`] = `
4+
[
5+
"requireResolve",
6+
"resolveGlobal",
7+
]
8+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { describe, expect, test } from 'vitest';
2+
3+
import * as m from './index';
4+
5+
describe('index', () => {
6+
test('index methods exist', () => {
7+
expect(Object.keys(m)).toMatchSnapshot();
8+
});
9+
});

packages/cspell-resolver/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { requireResolve, resolveGlobal } from './requireResolve.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as path from 'path';
2+
import { describe, expect, test } from 'vitest';
3+
4+
import { requireResolve, resolveGlobal } from './requireResolve.js';
5+
6+
describe('requireResolve', () => {
7+
test('requireResolve', () => {
8+
expect(requireResolve('./' + path.basename(__filename))).toEqual(__filename);
9+
});
10+
11+
test('resolveGlobal not found', () => {
12+
expect(resolveGlobal('not-found-module')).toBeUndefined();
13+
});
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { npm, yarn } from 'global-dirs';
2+
3+
export function resolveGlobal(modulesName: string): string | undefined {
4+
const paths = [npm.packages, yarn.packages];
5+
return requireResolve(modulesName, paths);
6+
}
7+
8+
export function requireResolve(filename: string, paths?: string[]): string | undefined {
9+
try {
10+
return require.resolve(filename, paths ? { paths } : undefined);
11+
} catch (e) {
12+
return undefined;
13+
}
14+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.cjs.json",
3+
"compilerOptions": {
4+
"module": "CommonJS",
5+
"moduleResolution": "node16",
6+
"outDir": "dist",
7+
"types": ["node"]
8+
},
9+
"include": ["src"]
10+
}

packages/cspell/.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/api
2-
/lib/*.map
3-
/lib/*.d.cts
4-
/lib/*.cjs
2+
/src/lib/*.map
3+
/src/lib/*.d.cts
4+
/src/lib/*.cjs
55
/dist.*/**
66
/esm
77
/dist

0 commit comments

Comments
 (0)