Skip to content

Commit

Permalink
Fix import.meta.url not being parsed as path properly (#1903)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
0237h and YaroShkvorets authored Jan 21, 2025
1 parent 27659e5 commit bcaad5e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-spiders-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

Fix `import.meta.url` not being parsed as path properly
4 changes: 3 additions & 1 deletion packages/cli/src/command-helpers/network.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as toolbox from 'gluegun';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import yaml from 'yaml';
import { initNetworksConfig, updateSubgraphNetwork } from './network.js';

const SUBGRAPH_PATH_BASE = path.join(
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]}`,
fileURLToPath(import.meta.url),
'..',
'..',
'..',
'..',
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChildProcess, spawn } from 'node:child_process';
import http from 'node:http';
import net from 'node:net';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import compose from 'docker-compose';
import { filesystem, patching } from 'gluegun';
import stripAnsi from 'strip-ansi';
Expand Down Expand Up @@ -93,9 +94,8 @@ export default class LocalCommand extends Command {
const composeFile =
composeFileFlag ||
path.join(
`${process.platform === 'win32' ? '' : '/'}${
/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]
}`,
fileURLToPath(import.meta.url),
'..',
'..',
'..',
'resources',
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/subgraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import fs from 'fs-extra';
import * as graphql from 'graphql/language/index.js';
import immutable from 'immutable';
Expand Down Expand Up @@ -53,7 +54,8 @@ export default class Subgraph {
const schema = graphql.parse(
await fs.readFile(
path.join(
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]}`,
fileURLToPath(import.meta.url),
'..',
'protocols',
// TODO: substreams/triggers is a special case, should be handled better
protocol.name === 'substreams/triggers' ? 'substreams' : protocol.name,
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const packageJson = JSON.parse(
fs
.readFileSync(
// works even when bundled/built because the path to package.json is the same
path.join(
`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)![1]}`,
'..',
'package.json',
),
path.join(fileURLToPath(import.meta.url), '..', '..', 'package.json'),
)
.toString(),
);
Expand Down

0 comments on commit bcaad5e

Please sign in to comment.