Skip to content

Commit b2ed0a3

Browse files
committed
Fix import.meta.url not being parsed as path properly
Using `import.meta.url` to resolve local file path and directories can lead to issues when special characters are percent-encoded in the url. This fix uses the `fileURLToPath` native method to properly parse local urls to file paths.
1 parent 27659e5 commit b2ed0a3

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

packages/cli/src/command-helpers/network.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
23
import * as toolbox from 'gluegun';
34
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
45
import yaml from 'yaml';
56
import { initNetworksConfig, updateSubgraphNetwork } from './network.js';
67

78
const SUBGRAPH_PATH_BASE = path.join(
8-
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]}`,
9+
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(fileURLToPath(import.meta.url))![1]}`,
910
'..',
1011
'..',
1112
'..',

packages/cli/src/commands/local.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ChildProcess, spawn } from 'node:child_process';
22
import http from 'node:http';
33
import net from 'node:net';
44
import path from 'node:path';
5+
import { fileURLToPath } from 'node:url';
56
import compose from 'docker-compose';
67
import { filesystem, patching } from 'gluegun';
78
import stripAnsi from 'strip-ansi';
@@ -94,7 +95,7 @@ export default class LocalCommand extends Command {
9495
composeFileFlag ||
9596
path.join(
9697
`${process.platform === 'win32' ? '' : '/'}${
97-
/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]
98+
/file:\/{2,3}(.+)\/[^/]/.exec(fileURLToPath(import.meta.url))![1]
9899
}`,
99100
'..',
100101
'..',

packages/cli/src/subgraph.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
23
import fs from 'fs-extra';
34
import * as graphql from 'graphql/language/index.js';
45
import immutable from 'immutable';
@@ -53,7 +54,7 @@ export default class Subgraph {
5354
const schema = graphql.parse(
5455
await fs.readFile(
5556
path.join(
56-
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]}`,
57+
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(fileURLToPath(import.meta.url))![1]}`,
5758
'protocols',
5859
// TODO: substreams/triggers is a special case, should be handled better
5960
protocol.name === 'substreams/triggers' ? 'substreams' : protocol.name,

packages/cli/src/version.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34

45
const packageJson = JSON.parse(
56
fs
67
.readFileSync(
78
// works even when bundled/built because the path to package.json is the same
89
path.join(
9-
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]}`,
10+
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(fileURLToPath(import.meta.url))![1]}`,
1011
'..',
1112
'package.json',
1213
),

0 commit comments

Comments
 (0)