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

fix(go): major version suffix is missing in module names for >=v2 #2507

Merged
merged 6 commits into from
Feb 2, 2021
Merged
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
6 changes: 4 additions & 2 deletions packages/jsii-pacmak/lib/targets/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ export class Golang extends Target {
// run `go build` with local.go.mod
await go('build', ['-modfile', localGoMod], { cwd: pkgDir });

// delete local.go.mod from the output directory so it doesn't get published
await fs.unlink(path.join(pkgDir, localGoMod));
// delete local.go.mod and local.go.sum from the output directory so it doesn't get published
const localGoSum = `${path.basename(localGoMod, '.mod')}.sum`;
await fs.remove(path.join(pkgDir, localGoMod));
await fs.remove(path.join(pkgDir, localGoSum));
}

/**
Expand Down
39 changes: 37 additions & 2 deletions packages/jsii-pacmak/lib/targets/go/package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CodeMaker } from 'codemaker';
import { Assembly, Type, Submodule as JsiiSubmodule } from 'jsii-reflect';
import { join } from 'path';
import * as semver from 'semver';

import { EmitContext } from './emit-context';
import { ReadmeFile } from './readme-file';
Expand Down Expand Up @@ -32,6 +33,7 @@ export abstract class Package {
public readonly packageName: string,
public readonly filePath: string,
public readonly moduleName: string,
public readonly version: string,
// If no root is provided, this module is the root
root?: Package,
) {
Expand Down Expand Up @@ -77,7 +79,8 @@ export abstract class Package {
const prefix = moduleName !== '' ? `${moduleName}/` : '';
const rootPackageName = this.root.packageName;
const suffix = this.filePath !== '' ? `/${this.filePath}` : ``;
return `${prefix}${rootPackageName}${suffix}`;
const versionSuffix = determineMajorVersionSuffix(this.version);
return `${prefix}${rootPackageName}${suffix}${versionSuffix}`;
}

/*
Expand Down Expand Up @@ -193,6 +196,7 @@ export class RootPackage extends Package {
packageName,
filePath,
moduleName,
assembly.version,
);

this.assembly = assembly;
Expand Down Expand Up @@ -285,7 +289,7 @@ export class RootPackage extends Package {
code.line('// Initialization endpoints of dependencies');
for (const pkg of dependencies) {
code.line(
`${pkg.packageName} "${pkg.root.moduleName}/${pkg.root.packageName}/${JSII_INIT_PACKAGE}"`,
`${pkg.packageName} "${pkg.root.goModuleName}/${JSII_INIT_PACKAGE}"`,
);
}
}
Expand Down Expand Up @@ -336,9 +340,40 @@ export class InternalPackage extends Package {
packageName,
filePath,
root.moduleName,
root.version,
root,
);

this.parent = parent;
}
}

/**
* Go requires that when a module major version is v2.0 and above, the module
* name will have a `/vNN` suffix (where `NN` is the major version).
*
* > Starting with major version 2, module paths must have a major version
* > suffix like /v2 that matches the major version. For example, if a module
* > has the path example.com/mod at v1.0.0, it must have the path
* > example.com/mod/v2 at version v2.0.0.
*
* @see https://golang.org/ref/mod#major-version-suffixes
* @param version The module version (e.g. `2.3.0`)
* @returns a suffix to append to the module name in the form (`/vNN`). If the
* module version is `0.x` or `1.x`, returns an empty string.
*/
function determineMajorVersionSuffix(version: string) {
const sv = semver.parse(version);
if (!sv) {
throw new Error(
`Unable to parse version "${version}" as a semantic version`,
);
}

// suffix is only needed for 2.0 and above
eladb marked this conversation as resolved.
Show resolved Hide resolved
if (sv.major <= 1) {
return '';
}

return `/v${sv.major}`;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.