Skip to content

Commit

Permalink
fix: normalize module path passed to runfiles helper for robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Nov 22, 2021
1 parent 85094e4 commit c9ccd21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/runfiles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class Runfiles {
}
/** Resolves the given module path. */
resolve(modulePath) {
// Normalize path by converting to forward slashes and removing all trailing
// forward slashes
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '');
if (path__default['default'].isAbsolute(modulePath)) {
return modulePath;
}
Expand All @@ -143,13 +146,19 @@ class Runfiles {
}
/** Resolves the given path relative to the current Bazel workspace. */
resolveWorkspaceRelative(modulePath) {
// Normalize path by converting to forward slashes and removing all trailing
// forward slashes
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '');
if (!this.workspace) {
throw new Error('workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set');
}
return this.resolve(path__default['default'].posix.join(this.workspace, modulePath));
}
/** Resolves the given path relative to the current Bazel package. */
resolvePackageRelative(modulePath) {
// Normalize path by converting to forward slashes and removing all trailing
// forward slashes
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '');
if (!this.workspace) {
throw new Error('workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set');
}
Expand Down
9 changes: 9 additions & 0 deletions packages/runfiles/runfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export class Runfiles {

/** Resolves the given module path. */
resolve(modulePath: string) {
// Normalize path by converting to forward slashes and removing all trailing
// forward slashes
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '')
if (path.isAbsolute(modulePath)) {
return modulePath;
}
Expand All @@ -128,6 +131,9 @@ export class Runfiles {

/** Resolves the given path relative to the current Bazel workspace. */
resolveWorkspaceRelative(modulePath: string) {
// Normalize path by converting to forward slashes and removing all trailing
// forward slashes
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '')
if (!this.workspace) {
throw new Error(
'workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set');
Expand All @@ -137,6 +143,9 @@ export class Runfiles {

/** Resolves the given path relative to the current Bazel package. */
resolvePackageRelative(modulePath: string) {
// Normalize path by converting to forward slashes and removing all trailing
// forward slashes
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '')
if (!this.workspace) {
throw new Error(
'workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set');
Expand Down

0 comments on commit c9ccd21

Please sign in to comment.