Skip to content

Commit d714099

Browse files
authored
fix(core): handle unique constraint errors when adding duplicate hashes to the cache db (#28310)
<!-- 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 --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent acb19a6 commit d714099

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

packages/nx/src/native/cache/cache.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl NxCache {
4848
workspace_root: PathBuf::from(workspace_root),
4949
cache_directory: cache_path.to_normalized_string(),
5050
cache_path,
51-
link_task_details: link_task_details.unwrap_or(true)
51+
link_task_details: link_task_details.unwrap_or(true),
5252
};
5353

5454
r.setup()?;
@@ -80,11 +80,7 @@ impl NxCache {
8080
"
8181
};
8282

83-
self.db
84-
.execute_batch(
85-
query,
86-
)
87-
.map_err(anyhow::Error::from)
83+
self.db.execute_batch(query).map_err(anyhow::Error::from)
8884
}
8985

9086
#[napi]
@@ -144,7 +140,7 @@ impl NxCache {
144140
create_dir_all(&task_dir)?;
145141

146142
// Write the terminal outputs into a file
147-
let task_outputs_path: _ = self.get_task_outputs_path_internal(&hash);
143+
let task_outputs_path = self.get_task_outputs_path_internal(&hash);
148144
trace!("Writing terminal outputs to: {:?}", &task_outputs_path);
149145
write(task_outputs_path, terminal_output)?;
150146

@@ -192,9 +188,7 @@ impl NxCache {
192188

193189
fn record_to_cache(&self, hash: String, code: i16) -> anyhow::Result<()> {
194190
self.db.execute(
195-
"INSERT INTO cache_outputs
196-
(hash, code)
197-
VALUES (?1, ?2)",
191+
"INSERT OR REPLACE INTO cache_outputs (hash, code) VALUES (?1, ?2)",
198192
params![hash, code],
199193
)?;
200194
Ok(())
@@ -258,19 +252,16 @@ impl NxCache {
258252
// Checks that the number of cache records in the database
259253
// matches the number of cache directories on the filesystem.
260254
// If they don't match, it means that the cache is out of sync.
261-
let cache_records_exist = self.db.query_row(
262-
"SELECT EXISTS (SELECT 1 FROM cache_outputs)",
263-
[],
264-
|row| {
265-
let exists: bool = row.get(0)?;
266-
Ok(exists)
267-
},
268-
)?;
255+
let cache_records_exist =
256+
self.db
257+
.query_row("SELECT EXISTS (SELECT 1 FROM cache_outputs)", [], |row| {
258+
let exists: bool = row.get(0)?;
259+
Ok(exists)
260+
})?;
269261

270262
if !cache_records_exist {
271263
let hash_regex = Regex::new(r"^\d+$").expect("Hash regex is invalid");
272-
let fs_entries = std::fs::read_dir(&self.cache_path)
273-
.map_err(anyhow::Error::from)?;
264+
let fs_entries = std::fs::read_dir(&self.cache_path).map_err(anyhow::Error::from)?;
274265

275266
for entry in fs_entries {
276267
let entry = entry?;

packages/nx/src/native/tests/cache.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ describe('Cache', () => {
5959
'output contents 123'
6060
);
6161
});
62+
63+
it('should handle storing hashes that already exist in the cache', async () => {
64+
cache.put('123', 'output 123', ['dist'], 0);
65+
expect(() => cache.put('123', 'output 123', ['dist'], 0)).not.toThrow();
66+
});
6267
});

0 commit comments

Comments
 (0)