Skip to content

Commit bcaad5e

Browse files
0237hYaroShkvorets
andauthored
Fix import.meta.url not being parsed as path properly (#1903)
* 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. * Add changeset * Remove regex and adjust path * remove unnecessary platform check --------- Co-authored-by: YaroShkvorets <[email protected]>
1 parent 27659e5 commit bcaad5e

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

.changeset/flat-spiders-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
Fix `import.meta.url` not being parsed as path properly

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
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+
fileURLToPath(import.meta.url),
10+
'..',
911
'..',
1012
'..',
1113
'..',

packages/cli/src/commands/local.ts

Lines changed: 3 additions & 3 deletions
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';
@@ -93,9 +94,8 @@ export default class LocalCommand extends Command {
9394
const composeFile =
9495
composeFileFlag ||
9596
path.join(
96-
`${process.platform === 'win32' ? '' : '/'}${
97-
/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]
98-
}`,
97+
fileURLToPath(import.meta.url),
98+
'..',
9999
'..',
100100
'..',
101101
'resources',

packages/cli/src/subgraph.ts

Lines changed: 3 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,8 @@ 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+
fileURLToPath(import.meta.url),
58+
'..',
5759
'protocols',
5860
// TODO: substreams/triggers is a special case, should be handled better
5961
protocol.name === 'substreams/triggers' ? 'substreams' : protocol.name,

packages/cli/src/version.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
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
8-
path.join(
9-
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]}`,
10-
'..',
11-
'package.json',
12-
),
9+
path.join(fileURLToPath(import.meta.url), '..', '..', 'package.json'),
1310
)
1411
.toString(),
1512
);

0 commit comments

Comments
 (0)