Skip to content

fix: buildVersion now correctly reads from project's package.json #3928

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

Merged
merged 3 commits into from
Jul 18, 2025
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
12 changes: 12 additions & 0 deletions .changeset/fix-build-version-package-json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@module-federation/manifest": patch
"@module-federation/managers": patch
"@module-federation/enhanced": patch
---

fix: BuildVersion now correctly reads from project's package.json

- Fixed getBuildVersion() to accept optional root parameter for correct directory resolution
- Updated StatsManager to use compiler.context when determining build version
- Ensures buildVersion in mf-manifest.json matches the project's package.json version
- Resolves issue #3835 where buildVersion was reading from wrong package.json location
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
it('should read buildVersion from package.json', () => {
const fs = require('fs');
const path = require('path');

// Read the generated mf-manifest.json file (Module Federation manifest)
const manifestPath = path.join(__dirname, 'mf-manifest.json');
expect(fs.existsSync(manifestPath)).toBe(true);

const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));

// Check that buildVersion matches package.json version
expect(manifest.metaData.buildInfo).toBeDefined();
expect(manifest.metaData.buildInfo.buildVersion).toBe('1.2.3');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test-build-version",
"version": "1.2.3",
"description": "Test case for BuildVersion reading from package.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'test module';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const ModuleFederationPlugin =
require('../../../../dist/src/lib/container/ModuleFederationPlugin').default;

module.exports = {
mode: 'development',
entry: './index.js',
devtool: false,
output: {
publicPath: 'http://localhost:3000/',
},
optimization: {
minimize: false,
},
stats: 'none',
plugins: [
new ModuleFederationPlugin({
name: 'build_version_test',
library: { type: 'commonjs-module' },
filename: 'remoteEntry.js',
exposes: {
'./test': './test.js',
},
manifest: true,
}),
],
};
4 changes: 2 additions & 2 deletions packages/managers/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ export function parseOptions<T, R>(
return items;
}

export function getBuildVersion(): string {
export function getBuildVersion(root?: string): string {
if (process.env['MF_BUILD_VERSION']) {
return process.env['MF_BUILD_VERSION'];
}
const pkg = new PKGJsonManager().readPKGJson();
const pkg = new PKGJsonManager().readPKGJson(root);
if (pkg?.['version'] && typeof pkg['version'] === 'string') {
return pkg['version'];
}
Expand Down
9 changes: 5 additions & 4 deletions packages/manifest/src/StatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ class StatsManager {
private _sharedManager: SharedManager = new SharedManager();
private _pkgJsonManager: PKGJsonManager = new PKGJsonManager();

get buildInfo(): StatsBuildInfo {
const pkg = this._pkgJsonManager.readPKGJson(process.cwd());
private getBuildInfo(context?: string): StatsBuildInfo {
const rootPath = context || process.cwd();
const pkg = this._pkgJsonManager.readPKGJson(rootPath);

return {
buildVersion: utils.getBuildVersion(),
buildVersion: utils.getBuildVersion(rootPath),
buildName: utils.getBuildName() || pkg['name'],
};
}
Expand All @@ -70,8 +71,8 @@ class StatsManager {
const { context } = compiler.options;
const {
_options: { name },
buildInfo,
} = this;
const buildInfo = this.getBuildInfo(context);
const type = this._pkgJsonManager.getExposeGarfishModuleType(
context || process.cwd(),
);
Expand Down