Skip to content

Commit f99339a

Browse files
authored
fix: node ES module + worker issue on Windows (#392)
close #389
1 parent 0fc5001 commit f99339a

File tree

11 files changed

+710
-693
lines changed

11 files changed

+710
-693
lines changed

.codesandbox/ci.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"node": "14",
2+
"node": "16",
33
"packages": [
44
"packages/*"
55
],

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ on:
66

77
jobs:
88
ci:
9-
name: Lint and Test with Node.js ${{ matrix.node }}
9+
name: Lint and Test with Node.js ${{ matrix.node }} on ${{ matrix.os }}
1010
strategy:
1111
matrix:
12+
os:
13+
- macos-latest
14+
- ubuntu-latest
15+
- windows-latest
1216
node:
1317
- 14
1418
- 16
1519
- 18
16-
runs-on: ubuntu-latest
20+
runs-on: ${{ matrix.os }}
1721
steps:
1822
- name: Checkout Repo
1923
uses: actions/checkout@v3
@@ -34,11 +38,13 @@ jobs:
3438
run: |
3539
yarn lint
3640
yarn test
41+
continue-on-error: ${{ matrix.os == 'windows-latest' }}
3742
env:
3843
EFF_NO_LINK_RULES: true
3944
PARSER_NO_WATCH: true
4045

4146
- name: Codecov
47+
if: matrix.os != 'windows-latest'
4248
run: |
4349
yarn global add codecov
4450
codecov

package.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
"build": "run-p build:*",
1414
"build:r": "r -f es2015",
1515
"build:ts": "tsc -b",
16-
"clean": "rimraf packages/*/{lib,*.tsbuildinfo} node_modules/@1stg/eslint-config/node_modules",
16+
"clean": "rimraf packages/*/{lib,*.tsbuildinfo}",
1717
"lint": "run-p lint:*",
1818
"lint:es": "eslint . --cache -f friendly",
1919
"lint:tsc": "tsc --noEmit",
20-
"postinstall": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
2120
"prelint": "yarn build",
21+
"prepare": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
2222
"prerelease": "yarn build",
2323
"release": "lerna publish --conventional-commits --create-release github --yes",
2424
"release-next": "lerna publish --conventional-prerelease --preid next --pre-dist-tag next --yes",
@@ -27,16 +27,16 @@
2727
},
2828
"devDependencies": {
2929
"@1stg/lib-config": "^6.0.0",
30-
"@types/eslint": "^8.4.1",
30+
"@types/eslint": "^8.4.2",
3131
"@types/eslint-plugin-markdown": "^2.0.0",
32-
"@types/jest": "^27.4.1",
33-
"@types/node": "^17.0.29",
34-
"@types/react": "^18.0.8",
32+
"@types/jest": "^27.5.0",
33+
"@types/node": "^17.0.31",
34+
"@types/react": "^18.0.9",
3535
"@types/unist": "^2.0.6",
3636
"lerna": "^4.0.0",
3737
"patch-package": "^6.4.7",
3838
"react": "^18.1.0",
39-
"ts-jest": "^28.0.0-next.3",
39+
"ts-jest": "^28.0.2",
4040
"ts-node": "^10.7.0",
4141
"type-coverage": "^2.21.1",
4242
"typescript": "^4.6.4"
@@ -66,9 +66,6 @@
6666
"^eslint-mdx$": "<rootDir>/packages/eslint-mdx/src",
6767
"^eslint-plugin-mdx$": "<rootDir>/packages/eslint-plugin-mdx/src"
6868
},
69-
"transform": {
70-
"^.+\\.tsx?$": "ts-jest/legacy"
71-
},
7269
"collectCoverage": true,
7370
"coverageThreshold": {
7471
"global": {

packages/eslint-mdx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"remark-mdx": "^2.1.1",
3535
"remark-parse": "^10.0.1",
3636
"remark-stringify": "^10.0.2",
37-
"synckit": "^0.7.0",
37+
"synckit": "^0.7.1",
3838
"tslib": "^2.4.0",
3939
"unified": "^10.1.2"
4040
}

packages/eslint-mdx/src/helpers.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable unicorn/no-await-expression-member */
2+
import fs from 'fs'
23
import path from 'path'
4+
import { pathToFileURL } from 'url'
35

46
import type { SourceLocation } from 'estree'
57
import { createSyncFn } from 'synckit'
@@ -64,6 +66,30 @@ export const arrayify = <T, R = T extends Array<infer S> ? S : T>(
6466
return arr
6567
}, [])
6668

69+
/**
70+
* Given a filepath, get the nearest path that is a regular file.
71+
* The filepath provided by eslint may be a virtual filepath rather than a file
72+
* on disk. This attempts to transform a virtual path into an on-disk path
73+
*/
74+
export const getPhysicalFilename = (
75+
filename: string,
76+
child?: string,
77+
): string => {
78+
try {
79+
if (fs.statSync(filename).isDirectory()) {
80+
return child || filename
81+
}
82+
} catch (err) {
83+
const { code } = err as { code: string }
84+
// https://github.com/eslint/eslint/issues/11989
85+
// Additionally, it seems there is no `ENOTDIR` code on Windows...
86+
if (code === 'ENOTDIR' || code === 'ENOENT') {
87+
return getPhysicalFilename(path.dirname(filename), filename)
88+
}
89+
}
90+
return filename
91+
}
92+
6793
/**
6894
* ! copied from https://github.com/just-jeb/angular-builders/blob/master/packages/custom-webpack/src/utils.ts#L53-L67
6995
*
@@ -91,10 +117,13 @@ export const loadEsmModule = <T>(modulePath: URL | string): Promise<T> =>
91117
* @returns
92118
*/
93119
export const loadModule = async <T>(modulePath: string): Promise<T> => {
120+
const esModulePath = path.isAbsolute(modulePath)
121+
? pathToFileURL(modulePath)
122+
: modulePath
94123
switch (path.extname(modulePath)) {
95124
/* istanbul ignore next */
96125
case '.mjs': {
97-
return (await loadEsmModule<{ default: T }>(modulePath)).default
126+
return (await loadEsmModule<{ default: T }>(esModulePath)).default
98127
}
99128
/* istanbul ignore next */
100129
case '.cjs': {
@@ -113,7 +142,7 @@ export const loadModule = async <T>(modulePath: string): Promise<T> => {
113142
// Load the ESM configuration file using the TypeScript dynamic import workaround.
114143
// Once TypeScript provides support for keeping the dynamic import this workaround can be
115144
// changed to a direct dynamic import.
116-
return (await loadEsmModule<{ default: T }>(modulePath)).default
145+
return (await loadEsmModule<{ default: T }>(esModulePath)).default
117146
}
118147

119148
throw err

packages/eslint-mdx/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './helpers'
22
export * from './parser'
3-
export * from './processor'
43
export * from './traverse'
54
export * from './types'

packages/eslint-mdx/src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
isMdxNode,
99
normalizePosition,
1010
performSyncWork,
11+
getPhysicalFilename,
1112
} from './helpers'
12-
import { getPhysicalFilename } from './processor'
1313
import { traverse } from './traverse'
1414
import type { ParserOptions, WorkerParseResult } from './types'
1515

packages/eslint-mdx/src/processor.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/eslint-mdx/src/worker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/consistent-type-imports */
22
/* eslint-disable unicorn/no-await-expression-member */
33
import path from 'path'
4+
import { pathToFileURL } from 'url'
45

56
import type { Token } from 'acorn'
67
import { cosmiconfig } from 'cosmiconfig'
@@ -207,9 +208,11 @@ runAsWorker(
207208
if (!TokenTranslator) {
208209
TokenTranslator = (
209210
await loadEsmModule<typeof import('espree/lib/token-translator')>(
210-
path.resolve(
211-
require.resolve('espree/package.json'),
212-
'../lib/token-translator.js',
211+
pathToFileURL(
212+
path.resolve(
213+
require.resolve('espree/package.json'),
214+
'../lib/token-translator.js',
215+
),
213216
),
214217
)
215218
).default

0 commit comments

Comments
 (0)