Skip to content

Commit

Permalink
Merge 'upstream/master' into 'core-remove-fs-extra'
Browse files Browse the repository at this point in the history
  • Loading branch information
ziebam committed Sep 20, 2024
2 parents 0e62d10 + a510b36 commit c34a976
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 8 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
],
"@nx/workspace/valid-command-object": "error"
}
},
{
"files": ["pnpm-lock.yaml"],
"parser": "./tools/eslint-rules/raw-file-parser.js",
"rules": {
"@nx/workspace/ensure-pnpm-lock-version": [
"error",
{ "version": "9.0" }
]
}
}
]
}
4 changes: 4 additions & 0 deletions nx-dev/feature-search/src/lib/algolia-search.global.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@apply overflow-hidden !important;
}

.DocSearch-VisuallyHiddenForAccessibility {
visibility: hidden;
}

body .DocSearch-Container {
@apply fixed left-0 top-0 z-[50] flex h-screen w-screen cursor-auto flex-col bg-black/10 p-4 backdrop-blur-sm sm:p-6 md:p-[10vh] lg:p-[12vh] dark:bg-white/10;
}
Expand Down
9 changes: 8 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@
}
},
"lint": {
"dependsOn": ["build-native", "^build-native"]
"dependsOn": [
"build-native",
"^build-native",
"@nx/nx-source:lint-pnpm-lock"
]
},
"lint-pnpm-lock": {
"cache": true
},
"e2e": {
"cache": true,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"preinstall": "node ./scripts/preinstall.js",
"test": "nx run-many -t test",
"e2e": "nx run-many -t e2e --projects ./e2e/*",
"build:wasm": "rustup override set nightly-2024-07-19 && rustup target add wasm32-wasip1-threads && WASI_SDK_PATH=\"$(pwd)/wasi-sdk-23.0-x86_64-linux\" CMAKE_BUILD_PARALLEL_LEVEL=2 LIBSQLITE3_FLAGS=\"-DLONGDOUBLE_TYPE=double\" pnpm exec nx run-many -t build-native-wasm && rustup override unset"
"build:wasm": "rustup override set nightly-2024-07-19 && rustup target add wasm32-wasip1-threads && WASI_SDK_PATH=\"$(pwd)/wasi-sdk-23.0-x86_64-linux\" CMAKE_BUILD_PARALLEL_LEVEL=2 LIBSQLITE3_FLAGS=\"-DLONGDOUBLE_TYPE=double\" pnpm exec nx run-many -t build-native-wasm && rustup override unset",
"lint-pnpm-lock": "eslint pnpm-lock.yaml"
},
"devDependencies": {
"@actions/core": "^1.10.0",
Expand Down Expand Up @@ -377,6 +378,7 @@
},
"nx": {
"includedScripts": [
"lint-pnpm-lock",
"echo",
"check-commit",
"check-format",
Expand Down
28 changes: 23 additions & 5 deletions packages/nx/src/native/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct NxCache {
workspace_root: PathBuf,
cache_path: PathBuf,
db: External<Connection>,
link_task_details: bool,
}

#[napi]
Expand All @@ -35,6 +36,7 @@ impl NxCache {
workspace_root: String,
cache_path: String,
db_connection: External<Connection>,
link_task_details: Option<bool>,
) -> anyhow::Result<Self> {
let cache_path = PathBuf::from(&cache_path);

Expand All @@ -46,6 +48,7 @@ impl NxCache {
workspace_root: PathBuf::from(workspace_root),
cache_directory: cache_path.to_normalized_string(),
cache_path,
link_task_details: link_task_details.unwrap_or(true)
};

r.setup()?;
Expand All @@ -54,18 +57,32 @@ impl NxCache {
}

fn setup(&self) -> anyhow::Result<()> {
self.db
.execute_batch(
"BEGIN;
let query = if self.link_task_details {
"BEGIN;
CREATE TABLE IF NOT EXISTS cache_outputs (
hash TEXT PRIMARY KEY NOT NULL,
code INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (hash) REFERENCES task_details (hash)
);
COMMIT;
",
COMMIT;
"
} else {
"BEGIN;
CREATE TABLE IF NOT EXISTS cache_outputs (
hash TEXT PRIMARY KEY NOT NULL,
code INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMIT;
"
};

self.db
.execute_batch(
query,
)
.map_err(anyhow::Error::from)
}
Expand Down Expand Up @@ -115,6 +132,7 @@ impl NxCache {
outputs: Vec<String>,
code: i16,
) -> anyhow::Result<()> {
trace!("PUT {}", &hash);
let task_dir = self.cache_path.join(&hash);

// Remove the task directory
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export declare class ImportResult {

export declare class NxCache {
cacheDirectory: string
constructor(workspaceRoot: string, cachePath: string, dbConnection: ExternalObject<Connection>)
constructor(workspaceRoot: string, cachePath: string, dbConnection: ExternalObject<Connection>, linkTaskDetails?: boolean | undefined | null)
get(hash: string): CachedResult | null
put(hash: string, terminalOutput: string, outputs: Array<string>, code: number): void
applyRemoteCacheResults(hash: string, result: CachedResult): void
Expand Down
5 changes: 5 additions & 0 deletions tools/eslint-rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
RULE_NAME as ensurePnpmLockVersionName,
rule as ensurePnpmLockVersion,
} from './rules/ensure-pnpm-lock-version';
import {
RULE_NAME as validCommandObjectName,
rule as validCommandObject,
Expand Down Expand Up @@ -34,5 +38,6 @@ module.exports = {
rules: {
[validSchemaDescriptionName]: validSchemaDescription,
[validCommandObjectName]: validCommandObject,
[ensurePnpmLockVersionName]: ensurePnpmLockVersion,
},
};
23 changes: 23 additions & 0 deletions tools/eslint-rules/raw-file-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* We have a custom lint rule for our pnpm-lock.yaml file and naturally ESLint does not natively know how to parse it.
* Rather than using a full yaml parser for this one case (which will need to spend time creating a real AST for the giant
* lock file), we can instead use a custom parser which just immediately returns a dummy AST and then build the reading of
* the lock file into the rule itself.
*/
module.exports = {
parseForESLint: (code) => ({
ast: {
type: 'Program',
loc: { start: 0, end: code.length },
range: [0, code.length],
body: [],
comments: [],
tokens: [],
},
services: { isPlain: true },
scopeManager: null,
visitorKeys: {
Program: [],
},
}),
};
109 changes: 109 additions & 0 deletions tools/eslint-rules/rules/ensure-pnpm-lock-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* This file sets you up with structure needed for an ESLint rule.
*
* It leverages utilities from @typescript-eslint to allow TypeScript to
* provide autocompletions etc for the configuration.
*
* Your rule's custom logic will live within the create() method below
* and you can learn more about writing ESLint rules on the official guide:
*
* https://eslint.org/docs/developer-guide/working-with-rules
*
* You can also view many examples of existing rules here:
*
* https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules
*/

import { ESLintUtils } from '@typescript-eslint/utils';
import { closeSync, openSync, readSync } from 'node:fs';

// NOTE: The rule will be available in ESLint configs as "@nx/workspace-ensure-pnpm-lock-version"
export const RULE_NAME = 'ensure-pnpm-lock-version';

export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: ``,
},
schema: [
{
type: 'object',
properties: {
version: {
type: 'string',
},
},
additionalProperties: false,
},
],
messages: {
unparseableLockfileVersion:
'Could not parse lockfile version from pnpm-lock.yaml, the file may be corrupted or the ensure-pnpm-lock-version lint rule may need to be updated.',
incorrectLockfileVersion:
'pnpm-lock.yaml has a lockfileVersion of {{version}}, but {{expectedVersion}} is required.',
},
},
defaultOptions: [],
create(context) {
// Read upon creation of the rule, the contents should not change during linting
const lockfileFirstLine = readFirstLineSync('pnpm-lock.yaml');
// Extract the version number, it will be a string in single quotes
const lockfileVersion = lockfileFirstLine.match(
/lockfileVersion:\s*'([^']+)'/
)?.[1];

const options = context.options as { version: string }[];
if (!Array.isArray(options) || options.length === 0) {
throw new Error('Expected an array of options with a version property');
}
const expectedLockfileVersion = options[0].version;
return {
Program(node) {
if (!lockfileVersion) {
context.report({
node,
messageId: 'unparseableLockfileVersion',
});
return;
}

if (lockfileVersion !== expectedLockfileVersion) {
context.report({
node,
messageId: 'incorrectLockfileVersion',
data: {
version: lockfileVersion,
expectedVersion: expectedLockfileVersion,
},
});
}
},
};
},
});

/**
* pnpm-lock.yaml is a huge file, so only read the first line as efficiently as possible
* for optimum linting performance.
*/
function readFirstLineSync(filePath: string) {
const BUFFER_SIZE = 64; // Optimized for the expected line length
const buffer = Buffer.alloc(BUFFER_SIZE);
let line = '';
let bytesRead: number;
let fd: number;
try {
fd = openSync(filePath, 'r');
bytesRead = readSync(fd, buffer, 0, BUFFER_SIZE, 0);
line = buffer.toString('utf8', 0, bytesRead).split('\n')[0];
} catch (err) {
throw err; // Re-throw to allow caller to handle
} finally {
if (fd !== undefined) {
closeSync(fd);
}
}
return line;
}

0 comments on commit c34a976

Please sign in to comment.