korbit-ai[bot] commented on code in PR #32261: URL: https://github.com/apache/superset/pull/32261#discussion_r1959398605
########## scripts/check-type.js: ########## @@ -0,0 +1,244 @@ +#!/usr/bin/env node + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// @ts-check +const { exit } = require("node:process"); +const { join, dirname, normalize, sep } = require("node:path"); +const { readdir, stat } = require("node:fs/promises"); +const { existsSync } = require("node:fs"); +const { chdir, cwd } = require("node:process"); +const { createRequire } = require("node:module"); + +const SUPERSET_ROOT = dirname(__dirname); +const PACKAGE_ARG_REGEX = /^package=/; +const EXCLUDE_DECLARATION_DIR_REGEX = /^excludeDeclarationDir=/; +const DECLARATION_FILE_REGEX = /\.d\.ts$/; + +void (async () => { + const args = process.argv.slice(2); + const { + matchedArgs: [packageArg, excludeDeclarationDirArg], + remainingArgs, + } = extractArgs(args, [PACKAGE_ARG_REGEX, EXCLUDE_DECLARATION_DIR_REGEX]); + + if (!packageArg) { + console.error("package is not specified"); + exit(1); + } + + const packageRootDir = await getPackage(packageArg); + const updatedArgs = removePackageSegment(remainingArgs, packageRootDir); + const argsStr = updatedArgs.join(" "); + + const excludedDeclarationDirs = getExcludedDeclarationDirs( + excludeDeclarationDirArg + ); + let declarationFiles = await getFilesRecursively( + packageRootDir, + DECLARATION_FILE_REGEX, + excludedDeclarationDirs + ); + declarationFiles = removePackageSegment(declarationFiles, packageRootDir); + const declarationFilesStr = declarationFiles.join(" "); + + const packageRootDirAbsolute = join(SUPERSET_ROOT, packageRootDir); + const tsConfig = getTsConfig(packageRootDirAbsolute); + const command = `--noEmit --allowJs --composite false --project ${tsConfig} ${argsStr} ${declarationFilesStr}`; + + try { + chdir(packageRootDirAbsolute); + // Please ensure that tscw-config is installed in the package being type-checked. + const tscw = packageRequire("tscw-config"); + const child = await tscw`${command}`; + + if (child.stdout) { + console.log(child.stdout); + } + + if (child.stderr) { + console.error(child.stderr); + } + + exit(child.exitCode); + } catch (e) { + console.error("Failed to execute type checking:", e); + console.error("Package:", packageRootDir); + console.error("Command:", `tscw ${command}`); + exit(1); + } +})(); + +/** + * @param {string} dir + * @param {RegExp} regex + * @param {string[]} excludedDirs + * + * @returns {Promise<string[]>} + */ + +async function getFilesRecursively(dir, regex, excludedDirs) { + try { + const files = await readdir(dir, { withFileTypes: true }); + const recursivePromises = []; + const result = []; + + for (const file of files) { + const fullPath = join(dir, file.name); + const shouldExclude = excludedDirs.some((excludedDir) => + normalize(fullPath).includes(normalize(excludedDir)) + ); + + if (file.isDirectory() && !shouldExclude) { + recursivePromises.push( + getFilesRecursively(fullPath, regex, excludedDirs) + ); + } else if (regex.test(file.name)) { + result.push(fullPath); + } + } + + const recursiveResults = await Promise.all(recursivePromises); Review Comment: ### Unbounded concurrent directory traversal <sub></sub> <details> <summary>Tell me more</summary> ###### What is the issue? The recursive directory traversal creates and awaits all promises simultaneously, which can lead to memory spikes when processing large directory trees. ###### Why this matters In large codebases with deep directory structures, this approach will create many promises in memory before any can complete, potentially causing out-of-memory issues or degraded performance. ###### Suggested change ∙ *Feature Preview* Implement a semaphore or concurrency limit to control the number of concurrent directory reads. Consider using a library like `p-limit` or implement a simple queue: ```javascript const pLimit = require('p-limit'); const limit = pLimit(10); // limit concurrent operations async function getFilesRecursively(dir, regex, excludedDirs) { const files = await readdir(dir, { withFileTypes: true }); const recursivePromises = []; const result = []; for (const file of files) { // ... existing checks ... if (file.isDirectory() && !shouldExclude) { recursivePromises.push( limit(() => getFilesRecursively(fullPath, regex, excludedDirs)) ); } // ... rest of the code ... } } ``` </details> <sub> [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/17acd6b3-c005-4a4f-98c0-35cdecad12c6?suggestedFixEnabled=true) 💬 Chat with Korbit by mentioning @korbit-ai. </sub> <!--- korbi internal id:6293afc8-2d66-481b-b400-4ecf457781fb --> ########## scripts/check-type.js: ########## @@ -0,0 +1,244 @@ +#!/usr/bin/env node + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// @ts-check +const { exit } = require("node:process"); +const { join, dirname, normalize, sep } = require("node:path"); +const { readdir, stat } = require("node:fs/promises"); +const { existsSync } = require("node:fs"); +const { chdir, cwd } = require("node:process"); +const { createRequire } = require("node:module"); + +const SUPERSET_ROOT = dirname(__dirname); +const PACKAGE_ARG_REGEX = /^package=/; +const EXCLUDE_DECLARATION_DIR_REGEX = /^excludeDeclarationDir=/; +const DECLARATION_FILE_REGEX = /\.d\.ts$/; + +void (async () => { + const args = process.argv.slice(2); + const { + matchedArgs: [packageArg, excludeDeclarationDirArg], + remainingArgs, + } = extractArgs(args, [PACKAGE_ARG_REGEX, EXCLUDE_DECLARATION_DIR_REGEX]); + + if (!packageArg) { + console.error("package is not specified"); + exit(1); + } + + const packageRootDir = await getPackage(packageArg); + const updatedArgs = removePackageSegment(remainingArgs, packageRootDir); + const argsStr = updatedArgs.join(" "); + + const excludedDeclarationDirs = getExcludedDeclarationDirs( + excludeDeclarationDirArg + ); + let declarationFiles = await getFilesRecursively( + packageRootDir, + DECLARATION_FILE_REGEX, + excludedDeclarationDirs + ); + declarationFiles = removePackageSegment(declarationFiles, packageRootDir); + const declarationFilesStr = declarationFiles.join(" "); + + const packageRootDirAbsolute = join(SUPERSET_ROOT, packageRootDir); + const tsConfig = getTsConfig(packageRootDirAbsolute); + const command = `--noEmit --allowJs --composite false --project ${tsConfig} ${argsStr} ${declarationFilesStr}`; + + try { + chdir(packageRootDirAbsolute); + // Please ensure that tscw-config is installed in the package being type-checked. + const tscw = packageRequire("tscw-config"); + const child = await tscw`${command}`; + + if (child.stdout) { + console.log(child.stdout); + } + + if (child.stderr) { + console.error(child.stderr); + } + + exit(child.exitCode); + } catch (e) { + console.error("Failed to execute type checking:", e); + console.error("Package:", packageRootDir); + console.error("Command:", `tscw ${command}`); + exit(1); + } +})(); + +/** + * @param {string} dir + * @param {RegExp} regex + * @param {string[]} excludedDirs + * + * @returns {Promise<string[]>} + */ + +async function getFilesRecursively(dir, regex, excludedDirs) { + try { + const files = await readdir(dir, { withFileTypes: true }); + const recursivePromises = []; + const result = []; + + for (const file of files) { + const fullPath = join(dir, file.name); + const shouldExclude = excludedDirs.some((excludedDir) => + normalize(fullPath).includes(normalize(excludedDir)) + ); Review Comment: ### Directory exclusion logic false positives <sub></sub> <details> <summary>Tell me more</summary> ###### What is the issue? The directory exclusion logic using includes() can lead to false positives. For example, if '/test' is an excluded directory, it would also exclude '/test-utils'. ###### Why this matters This can result in unexpected behavior where files that should be type-checked are skipped, potentially missing type errors in the codebase. ###### Suggested change ∙ *Feature Preview* Replace the includes check with a more precise path matching using sep character or regex boundaries: ```javascript const shouldExclude = excludedDirs.some((excludedDir) => { const normalizedExcludedDir = normalize(excludedDir); const normalizedFullPath = normalize(fullPath); return normalizedFullPath === normalizedExcludedDir || normalizedFullPath.startsWith(normalizedExcludedDir + sep); }); ``` </details> <sub> [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/90c46927-4410-489c-8ca9-3ba0271b88f1?suggestedFixEnabled=true) 💬 Chat with Korbit by mentioning @korbit-ai. </sub> <!--- korbi internal id:c4fc596e-be3c-4877-a25d-f7c70d3e9aaf --> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org