Skip to content

Commit

Permalink
fix: improve package.json lookup for root name (#1530)
Browse files Browse the repository at this point in the history
compileProtos seeks for package.json one directory above the one it
accepts as input (typically src).

Running gapic-generator-typescript with --format=esm, generates the
the sources in esm/src, then compileProtos can't find package.json.

When package.json is not found, the root name falls back to default
and all the packages have the same root.

Use walk-up-path, which is also used by npm[1].

Fixes #1529.

[1] https://github.com/npm/config/blob/77a48dbe22/lib/index.js#L632

Co-authored-by: sofisl <55454395+sofisl@users.noreply.github.com>
  • Loading branch information
orgads and sofisl committed Mar 23, 2024
1 parent 45075c6 commit 3ec09cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
5 changes: 3 additions & 2 deletions tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
"author": "Google API Authors",
"license": "Apache-2.0",
"dependencies": {
"@babel/core": "^7.22.5",
"@babel/traverse": "^7.22.5",
"google-gax": "^4.1.0",
"google-proto-files": "^4.1.0",
"protobufjs-cli": "1.1.2",
"rimraf": "^5.0.1",
"@babel/core": "^7.22.5",
"@babel/traverse": "^7.22.5",
"uglify-js": "^3.17.0",
"walk-up-path": "^3.0.1",
"walkdir": "^0.4.0"
},
"repository": {
Expand Down
19 changes: 11 additions & 8 deletions tools/src/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as path from 'path';
import * as util from 'util';
import * as pbjs from 'protobufjs-cli/pbjs';
import * as pbts from 'protobufjs-cli/pbts';
import {walkUp} from 'walk-up-path';

export const gaxProtos = path.join(
require.resolve('google-gax'),
Expand Down Expand Up @@ -360,14 +361,16 @@ export async function generateRootName(directories: string[]): Promise<string> {
// It's OK to play some guessing game here: if we locate `package.json`
// with a package name, we'll use it; otherwise, we'll fallback to 'default'.
for (const directory of directories) {
const packageJson = path.resolve(directory, '..', 'package.json');
if (fs.existsSync(packageJson)) {
const json = JSON.parse((await readFile(packageJson)).toString()) as {
name: string;
};
const name = json.name.replace(/[^\w\d]/g, '_');
const hopefullyUniqueName = `${name}_protos`;
return hopefullyUniqueName;
for (const p of walkUp(path.resolve(directory, '..'))) {
const packageJson = path.join(p, 'package.json');
if (fs.existsSync(packageJson)) {
const json = JSON.parse((await readFile(packageJson)).toString()) as {
name: string;
};
const name = json.name.replace(/[^\w\d]/g, '_');
const hopefullyUniqueName = `${name}_protos`;
return hopefullyUniqueName;
}
}
}
return 'default';
Expand Down
9 changes: 8 additions & 1 deletion tools/test/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,17 @@ describe('compileProtos tool', () => {
assert.strictEqual(rootName, '_org_fake_package_protos');
});

it('falls back to the default name for protobuf root if unable to guess', async () => {
it('uses the nearest package.json to guess the root name', async () => {
const rootName = await compileProtos.generateRootName([
path.join(__dirname, 'protoLists', 'empty'),
]);
assert.strictEqual(rootName, 'gapic_tools_protos');
});

it('falls back to the default name for protobuf root if unable to guess', async () => {
const rootName = await compileProtos.generateRootName([
'/nonexistent/empty',
]);
assert.strictEqual(rootName, 'default');
});

Expand Down

0 comments on commit 3ec09cf

Please sign in to comment.