Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows imports with spaces #187

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/imports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"debug": "^3.1.0",
"hosted-git-info": "^2.6.0",
"path-browserify": "^1.0.0",
"url": "^0.11.0"
"url": "^0.11.0",
"is-url": "^1.2.4"
}
}
26 changes: 23 additions & 3 deletions packages/imports/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ResolverEngine } from "@resolver-engine/core";
import isUrl from "is-url";
import pathSys from "path";
import urlSys from "url";
import { ImportFile } from "./parsers/importparser";
Expand All @@ -25,6 +26,25 @@ export function findImports(data: ImportFile): string[] {
return result;
}

function resolvePath(workingDir: string, relativePath: string): string {
// If the working dir is an URL (a file imported from an URL location)
// or the relative path is an URL (an URL imported from a local file)
// use url.resolve, which will work in both cases.
// > url.resolve('/local/folder/', 'http://example.com/myfile.sol')
// 'http://example.com/myfile.sol'
// > url.resolve('http://example.com/', 'myfile.sol')
// 'http://example.com/myfile.sol'
//
// If not, use path.resolve, since using url.resolve will escape certain
// charaters (e.g. a space as an %20), breaking the path
// > url.resolve('/local/path/with spaces/', 'myfile.sol')
// '/local/path/with%20spaces/myfile.sol'

return isUrl(workingDir) || isUrl(relativePath)
? urlSys.resolve(workingDir, relativePath)
: pathSys.resolve(pathSys.dirname(workingDir), relativePath);
}

interface ImportTreeNode extends ImportFile {
uri: string;
// uri and url the same as in rest of resolver-engine
Expand Down Expand Up @@ -105,7 +125,7 @@ export async function gatherSources(
if (workingDir !== "") {
workingDir += "/";
}
const absoluteRoots = roots.map(what => urlSys.resolve(workingDir, what));
const absoluteRoots = roots.map(what => resolvePath(workingDir, what));
for (const absWhat of absoluteRoots) {
queue.push({ cwd: workingDir, file: absWhat, relativeTo: workingDir });
alreadyImported.add(absWhat);
Expand All @@ -120,7 +140,7 @@ export async function gatherSources(
// if not - return the same name it was imported with
let relativePath: string;
if (fileData.file[0] === ".") {
relativePath = urlSys.resolve(fileData.relativeTo, fileData.file);
relativePath = resolvePath(fileData.relativeTo, fileData.file);
result.push({ url: relativePath, source: resolvedFile.source, provider: resolvedFile.provider });
} else {
relativePath = fileData.file;
Expand All @@ -131,7 +151,7 @@ export async function gatherSources(
for (const foundImport of foundImports) {
let importName: string;
if (foundImport[0] === ".") {
importName = urlSys.resolve(relativePath, foundImport);
importName = resolvePath(relativePath, foundImport);
} else {
importName = foundImport;
}
Expand Down