Skip to content

Commit

Permalink
Add version stamping for wsl-helper
Browse files Browse the repository at this point in the history
Closes rancher-sandbox#5808

Signed-off-by: Carlos Barcenilla <[email protected]>
  • Loading branch information
bcxpro committed Feb 25, 2025
1 parent 4612cd8 commit fea7bcb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
64 changes: 60 additions & 4 deletions scripts/dependencies/go-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ type GoDependencyOptions = {
* Additional environment for the go compiler; e.g. for GOARCH overrides.
*/
env?: NodeJS.ProcessEnv;

/**
* The version string to be stamped into the binary at build time.
* This is typically used with `-ldflags="-X ..."` to embed version information.
* Example: `1.18.1`.
*/
version?: string;

/**
* The Go module path, typically as defined in `go.mod`. This should match the
* import path of the module (e.g., `github.com/rancher-sandbox/rancher-desktop/src/go/wsl-helper`).
*/
modulePath?: string;

/**
* Custom linker flags (`-ldflags`) for the Go build process.
* The default value includes `-s -w`, which disables symbol table and debug information
* to reduce binary size. Override this to add or modify linker flags.
* Example: `['-s', '-w']`
*/
ldFlags?: string[]

/**
* Custom compiler flags (`-gcflags`) for the Go build process.
* By default, no additional compiler flags are included. Use this to pass
* specific flags to the Go compiler, such as optimizations or debugging options.
* Example: `['all=-N', '-l']` to disable optimizations and inlining for all
* packages, which is useful for debugging.
*/
gcFlags?: string[]
};

/**
Expand Down Expand Up @@ -49,8 +79,29 @@ export class GoDependency implements Dependency {
const sourceDir = path.join(process.cwd(), 'src', 'go', this.sourcePath);
const outFile = this.outFile(context);

console.log(`Building go utility \x1B[1;33;40m${ this.name }\x1B[0m from ${ sourceDir } to ${ outFile }...`);
await simpleSpawn('go', ['build', '-ldflags', '-s -w', '-o', outFile, '.'], {
const buildArgs: string[] = ['build'];

// Handle `-ldflags`: use custom flags if provided (default: `-s -w`),
// append version stamp if `modulePath` and `version` exist, and skip if no flags.
const ldFlags: string[] = this.options.ldFlags ? [...this.options.ldFlags] : ['-s', '-w'];

if (this.options.version && this.options.modulePath) {
ldFlags.push(`-X ${ this.options.modulePath }/pkg/version.Version=${ this.options.version }`);
}
if (ldFlags.length > 0) {
buildArgs.push('-ldflags', ldFlags.join(' '));
}

// Handle compiler flags (`-gcflags`). Only include `-gcflags` if explicitly provided and not empty.
if (this.options.gcFlags && this.options.gcFlags.length > 0) {
buildArgs.push('-gcflags', this.options.gcFlags.join(' '));
}

// common args
buildArgs.push('-o', outFile, '.');

console.log(`Building go utility \x1B[1;33;40m${ this.name } \x1B[0m from ${ sourceDir } to ${ outFile }...`);
await simpleSpawn('go', buildArgs, {
cwd: sourceDir,
env: this.environment(context),
});
Expand Down Expand Up @@ -112,8 +163,13 @@ export class RDCtl extends GoDependency {
}

export class WSLHelper extends GoDependency {
constructor() {
super('wsl-helper', { outputPath: 'internal', env: { CGO_ENABLED: '0' } });
constructor(version: string) {
super('wsl-helper', {
outputPath: 'internal',
env: { CGO_ENABLED: '0' },
modulePath: 'github.com/rancher-sandbox/rancher-desktop/src/go/wsl-helper',
version,
});
}

dependencies(context: DownloadContext): string[] {
Expand Down
25 changes: 23 additions & 2 deletions scripts/postinstall.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import childProcess from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
Expand Down Expand Up @@ -25,6 +26,14 @@ type DependencyWithContext = {
*/
const InstallTimeout = 10 * 60 * 1_000; // Ten minutes.

/**
* Retrieves the application version from package.json to stamp Go binaries.
* This version number ensures Go utilities like WSL helpers are tagged with
* the same version as the main application, maintaining consistency across
* all components of Rancher Desktop.
*/
const versionToStamp = getStampVersion();

// Dependencies that should be installed into places that users touch
// (so users' WSL distros and hosts as of the time of writing).
const userTouchedDependencies = [
Expand Down Expand Up @@ -59,7 +68,7 @@ const windowsDependencies = [
new WSLDistroImage(),
new Wix(),
new goUtils.GoDependency('networking/cmd/host', 'internal/host-switch'),
new goUtils.WSLHelper(),
new goUtils.WSLHelper(versionToStamp),
new goUtils.NerdctlStub(),
new goUtils.SpinStub(),
];
Expand All @@ -72,7 +81,7 @@ const wslDependencies = [
new goUtils.GoDependency('networking/cmd/vm', 'staging/vm-switch'),
new goUtils.GoDependency('networking/cmd/network', 'staging/network-setup'),
new goUtils.GoDependency('networking/cmd/proxy', 'staging/wsl-proxy'),
new goUtils.WSLHelper(),
new goUtils.WSLHelper(versionToStamp),
new goUtils.NerdctlStub(),
];

Expand Down Expand Up @@ -254,3 +263,15 @@ const keepScriptAlive = setTimeout(() => { }, 24 * 3600 * 1000);
process.exit(exitCode);
}
})();

/**
* Gets the version string for Go tools from git.
* Format: {tag}-{commits}-{hash}{dirty}
* Examples: v1.18.0, v1.18.0-39-gf46609959, v1.18.0-39-gf46609959.m
*/
function getStampVersion(): string {
const gitCommand = 'git describe --match v[0-9]* --dirty=.m --always --tags';
const stdout = childProcess.execSync(gitCommand, { encoding: 'utf-8' });

return stdout;
}

0 comments on commit fea7bcb

Please sign in to comment.