Skip to content

Comments

fix: make process.mainModule writable#18730

Closed
DonIsaac wants to merge 6 commits intomainfrom
don/fix/process-main-module
Closed

fix: make process.mainModule writable#18730
DonIsaac wants to merge 6 commits intomainfrom
don/fix/process-main-module

Conversation

@DonIsaac
Copy link
Contributor

@DonIsaac DonIsaac commented Apr 3, 2025

What does this PR do?

Add a setter for process.mainModule. This snippet works in node but not in bun, causing some older test runners to break

process.mainModule = createMockSomehow(process.mainModule)

How did you verify your code works?

I've added tests

@DonIsaac DonIsaac added the node.js Compatibility with Node.js APIs label Apr 3, 2025
@robobun
Copy link
Collaborator

robobun commented Apr 3, 2025

Updated 5:27 PM PT - Apr 9th, 2025

@Jarred-Sumner, your commit 7633980 has 1 failures in Build #14750:


🧪   try this PR locally:

bunx bun-pr 18730

@DonIsaac DonIsaac requested review from a team and cirospaciari and removed request for a team April 3, 2025 01:16
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The private variable is unnecessary, and calling BunObject__getter_wrap_main is non-trivial overhead

Instead, we can add a LazyProperty onto Zig::GlobalObject and make Bun.main access that LazyProperty and the process.mainModule getter & setter can get/set that LazyProperty

@DonIsaac DonIsaac requested a review from Jarred-Sumner April 7, 2025 22:20
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments

}

// NOTE: we cannot assume `main` is ascii.
var main_module = bun.String.init(bun_vm.main);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var main_module = bun.String.init(bun_vm.main);
return bun.String.createUTF8ForJS(bun_vm.main);

InternalModuleRegistry::createStructure(init.vm, init.owner)));
});

m_mainModule.initLater(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this visited?

return JSValue::encode(jsUndefined());
}
auto prop = PropertyName(JSC::Identifier::fromString(vm, WTFMove(mm->getString(global))));
auto mainModule = requireCache->getIfPropertyExists(global, prop);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this accessing the require object? Why isn't this a getter/setter for the same main module LazyProperty defined on the global object? The require.main and the process.mainModule implementations should both access the same underlying WriteBarrier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

node.js Compatibility with Node.js APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants