Skip to content

Commit

Permalink
Delete .github/workflows/stale.yml (bazel-contrib#3769)
Browse files Browse the repository at this point in the history
In bazel-contrib#3714 it was clear that we're just not active enough in this repo to claim that a long silence means something is stale.
  • Loading branch information
alexeagle authored and jbedard committed Aug 19, 2024
1 parent ab85b37 commit e78f16f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/runfiles/paths.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// NB: on windows thanks to legacy 8-character path segments it might be like
// c:/b/ojvxx6nx/execroot/build_~1/bazel-~1/x64_wi~1/bin/internal/npm_in~1/test
export const BAZEL_OUT_REGEX = /(\/bazel-out\/|\/bazel-~1\/x64_wi~1\/)/;

export const REPO_MAPPINGS = "_repo_mapping";
61 changes: 52 additions & 9 deletions packages/runfiles/runfiles.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import * as fs from 'fs';
import * as path from 'path';
import * as fs from "fs";
import * as path from "path";

import {BAZEL_OUT_REGEX} from './paths';
import { BAZEL_OUT_REGEX, REPO_MAPPINGS } from "./paths";

/**
* Class that provides methods for resolving Bazel runfiles.
*/
export class Runfiles {
manifest: Map<string, string>|undefined;
runfilesDir: string|undefined;
manifest: Map<string, string> | undefined;
runfilesDir: string | undefined;
/**
* If the environment gives us enough hints, we can know the workspace name
*/
workspace: string|undefined;
workspace: string | undefined;
/**
* If the environment gives us enough hints, we can know the package path
*/
package: string|undefined;
package: string | undefined;
/**
* If the environment has repo mappings, we can use them to resolve repo relative paths.
*/
repoMappings: Map<string, string> | undefined;

constructor(private _env: typeof process.env) {
// If Bazel sets a variable pointing to a runfiles manifest,
Expand All @@ -29,8 +33,10 @@ export class Runfiles {
this.manifest = this.loadRunfilesManifest(_env['RUNFILES_MANIFEST_FILE']!);
} else if (!!_env['RUNFILES_DIR']) {
this.runfilesDir = path.resolve(_env['RUNFILES_DIR']!);
this.repoMappings = this.loadRepoMapping(this.runfilesDir);
} else if (!!_env['RUNFILES']) {
this.runfilesDir = path.resolve(_env['RUNFILES']!);
this.repoMappings = this.loadRepoMapping(this.runfilesDir);
} else {
throw new Error(
'Every node program run under Bazel must have a $RUNFILES_DIR, $RUNFILES or $RUNFILES_MANIFEST_FILE environment variable');
Expand Down Expand Up @@ -116,6 +122,26 @@ export class Runfiles {
return runfilesEntries;
}

loadRepoMapping(runfilesDir: string): Map<string, string> {
const repoMappingPath = path.join(runfilesDir, REPO_MAPPINGS);
const repoMappings = new Map<string, string>()

if (fs.existsSync(repoMappingPath)) {
const mappings = fs.readFileSync(repoMappingPath, {encoding: 'utf-8'});
for (const line of mappings.split('\n')) {
if (!line) continue;
const [from, repoName, repoPath] = line.split(',');

// TODO: from !== ''?
if (from === '') {
repoMappings.set(repoName, repoPath);
}
}
}

return repoMappings;
}

/** Resolves the given module path. */
resolve(modulePath: string) {
// Normalize path by converting to forward slashes and removing all trailing
Expand All @@ -142,7 +168,11 @@ export class Runfiles {
throw new Error(
'workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set');
}
return this.resolve(path.posix.join(this.workspace, modulePath));
let workspace = this.workspace;
if (this.repoMappings && this.repoMappings.has(workspace)) {
workspace = this.repoMappings.get(workspace);
}
return this._resolve(workspace, modulePath);
}

/** Resolves the given path relative to the current Bazel package. */
Expand All @@ -159,7 +189,11 @@ export class Runfiles {
throw new Error(
'package could not be determined from the environment; make sure BAZEL_TARGET is set');
}
return this.resolve(path.posix.join(this.workspace, this.package, modulePath));
let workspace = this.workspace;
if (this.repoMappings && this.repoMappings.has(workspace)) {
workspace = this.repoMappings.get(workspace);
}
return this._resolve(workspace, path.posix.join(this.package, modulePath));
}

/**
Expand Down Expand Up @@ -189,6 +223,15 @@ export class Runfiles {
}
}
}
if (this.repoMappings && this.repoMappings.has(moduleBase)) {
const mappedRepo = this.repoMappings.get(moduleBase)
if (mappedRepo !== moduleBase) {
const maybe = this._resolve(mappedRepo, moduleTail)
if (maybe !== undefined) {
return maybe;
}
}
}
if (this.runfilesDir) {
const maybe = path.join(this.runfilesDir, moduleBase, moduleTail || '');
if (fs.existsSync(maybe)) {
Expand Down

0 comments on commit e78f16f

Please sign in to comment.