fix(core): ensure that setWorkspaceContext is run only on main nx packages (#28706)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
When running postinstall with Nx, we create a files archive file. When
there are multiple versions of nx installed, the post install would run
for all versions. This causes issues where one version might overwrite
the archive files while another version is writing to the same location.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
The `setupWorkspaceContext` is now only ran when we detect that it is
the main nx package. This also makes the creation of the archive file
more atomic so that we dont overwrite files while its being written by
other processes

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Jonathan Cammisuli 2024-11-04 21:23:20 -05:00 committed by GitHub
parent bb878aa033
commit b925f8ccfa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View File

@ -8,16 +8,15 @@ import { verifyOrUpdateNxCloudClient } from '../src/nx-cloud/update-manager';
import { getCloudOptions } from '../src/nx-cloud/utilities/get-cloud-options';
import { isNxCloudUsed } from '../src/utils/nx-cloud-utils';
import { readNxJson } from '../src/config/nx-json';
import { setupWorkspaceContext } from '../src/utils/workspace-context';
import { logger } from '../src/utils/logger';
import { setupWorkspaceContext } from '../src/utils/workspace-context';
(async () => {
const start = new Date();
try {
setupWorkspaceContext(workspaceRoot);
if (isMainNxPackage() && fileExists(join(workspaceRoot, 'nx.json'))) {
assertSupportedPlatform();
setupWorkspaceContext(workspaceRoot);
try {
await daemonClient.stop();
} catch (e) {}

View File

@ -73,16 +73,41 @@ pub fn read_files_archive<P: AsRef<Path>>(cache_dir: P) -> Option<NxFileHashes>
pub fn write_files_archive<P: AsRef<Path>>(cache_dir: P, files: NxFileHashes) {
let now = std::time::Instant::now();
let archive_path = cache_dir.as_ref().join(NX_FILES_ARCHIVE);
let archive_path_temp =
cache_dir
.as_ref()
.join(format!("{}.{}", NX_FILES_ARCHIVE, std::process::id()));
std::fs::create_dir_all(&cache_dir)
.inspect_err(|e| {
trace!("Error creating cache directory: {:?}", e);
})
.ok();
let result = rkyv::to_bytes::<_, 2048>(&files)
.map_err(anyhow::Error::from)
.and_then(|encoded| {
std::fs::write(archive_path, encoded)?;
Ok(())
std::fs::write(&archive_path_temp, encoded).map_err(|e| {
anyhow::anyhow!(
"Unable to write to {}: {:?}",
&archive_path_temp.display(),
e
)
})
})
.and_then(|_| {
std::fs::rename(&archive_path_temp, &archive_path).map_err(|e| {
anyhow::anyhow!(
"unable to move temp archive file to {}: {:?}",
&archive_path.display(),
e
)
})
});
match result {
Ok(_) => {
trace!("write archive in {:?}", now.elapsed());
trace!("wrote archive in {:?}", now.elapsed());
}
Err(e) => {
trace!("could not write files archive: {:?}", e);