Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get entrypoint file #6200

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reverse all main entrypoint files in workspace
  • Loading branch information
chunyu3 committed Feb 7, 2025
commit 98d72bc1e129f65de6ded556090bb7b70e8aa718
81 changes: 59 additions & 22 deletions packages/typespec-vscode/src/typespec-utils.ts
Copy link
Member

Choose a reason for hiding this comment

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

Can you document what gets resolved here?

Does it now only look for a pacakge.json first and then a main.tsp or it just look up for the first it finds

Original file line number Diff line number Diff line change
@@ -11,28 +11,9 @@ export async function getEntrypointTspFile(tspPath: string): Promise<string | un
let baseDir = isFilePath ? getDirectoryPath(tspPath) : tspPath;

while (true) {
const pkgPath = path.resolve(baseDir, "package.json");
if (await isFile(pkgPath)) {
/* get the tspMain from package.json. */
try {
const data = await readFile(pkgPath, { encoding: "utf-8" });
const packageJson = JSON.parse(data);
const tspMain = packageJson.tspMain;
if (typeof tspMain === "string") {
const tspMainFile = path.resolve(baseDir, tspMain);
if (await isFile(tspMainFile)) {
logger.debug(`tspMain file ${tspMainFile} selected as entrypoint file.`);
return tspMainFile;
}
}
} catch (error) {
logger.error(`An error occurred while reading the package.json file ${pkgPath}`, [error]);
}
}

const mainTspFile = path.resolve(baseDir, StartFileName);
if (await isFile(mainTspFile)) {
return mainTspFile;
const entrypointTspFile = await getEntrypointTspFileInFolder(baseDir);
if (entrypointTspFile) {
return entrypointTspFile;
}
const parentDir = getDirectoryPath(baseDir);
if (parentDir === baseDir) {
@@ -41,6 +22,9 @@ export async function getEntrypointTspFile(tspPath: string): Promise<string | un
baseDir = parentDir;
}

/* if there is no defined main entry point tsp file, return the selected tsp file. */
if (isFilePath) return tspPath;

return undefined;
}

@@ -53,3 +37,56 @@ export async function TraverseMainTspFileInWorkspace() {
.map((uri) => normalizeSlashes(uri.fsPath)),
);
}

export async function TraverseEntrypointTspFileInWorkspace(): Promise<string[]> {
const targetFolders = await vscode.workspace
.findFiles(`**/${StartFileName}`, "**/node_modules/**")
.then((uris) => {
const directories = new Set<string>();
uris
.filter((uri) => uri.scheme === "file" && !uri.fsPath.includes("node_modules"))
.map((uri) => directories.add(getDirectoryPath(normalizeSlashes(uri.fsPath))));
return Array.from(directories);
});
const entrypointTspFiles: string[] = [];

for (const folder of targetFolders) {
while (true) {
const entrypointTspFile = await getEntrypointTspFileInFolder(folder);
if (entrypointTspFile) {
entrypointTspFiles.push(entrypointTspFile);
}
}
}

return entrypointTspFiles;
}

async function getEntrypointTspFileInFolder(folder: string): Promise<string | undefined> {
Copy link
Member

Choose a reason for hiding this comment

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

you shouldn't rewrite that this is not the actual logic, you are missing exports which are a very complex resolution
We should share this logic from the compiler

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. @timotheeguerin , maybe you can help to also expose the "resolveTypeSpecEntrypoint" too when you are separating the PR for init bundling if it's easy. thx.

if (await isFile(folder)) return undefined;
const pkgPath = path.resolve(folder, "package.json");
if (await isFile(pkgPath)) {
/* get the tspMain from package.json. */
try {
const data = await readFile(pkgPath, { encoding: "utf-8" });
const packageJson = JSON.parse(data);
const tspMain = packageJson.tspMain;
if (typeof tspMain === "string") {
const tspMainFile = path.resolve(folder, tspMain);
if (await isFile(tspMainFile)) {
logger.debug(`tspMain file ${tspMainFile} selected as entrypoint file.`);
return tspMainFile;
}
}
} catch (error) {
logger.error(`An error occurred while reading the package.json file ${pkgPath}`, [error]);
}
}

const mainTspFile = path.resolve(folder, StartFileName);
if (await isFile(mainTspFile)) {
return mainTspFile;
}

return undefined;
}
Original file line number Diff line number Diff line change
@@ -8,7 +8,10 @@ import logger from "../../log/logger.js";
import { InstallAction, npmDependencyType, NpmUtil } from "../../npm-utils.js";
import { getDirectoryPath } from "../../path-utils.js";
import { resolveTypeSpecCli } from "../../tsp-executable-resolver.js";
import { getEntrypointTspFile, TraverseMainTspFileInWorkspace } from "../../typespec-utils.js";
import {
getEntrypointTspFile,
TraverseEntrypointTspFileInWorkspace,
} from "../../typespec-utils.js";
import { ExecOutput, isFile, spawnExecutionAndLogToOutput } from "../../utils.js";
import { EmitQuickPickItem } from "./emit-quick-pick-item.js";
import {
@@ -385,7 +388,7 @@ async function doEmit(mainTspFile: string, emitter: Emitter) {
export async function emitCode(context: vscode.ExtensionContext, uri: vscode.Uri) {
let tspProjectFile: string = "";
if (!uri) {
const targetPathes = await TraverseMainTspFileInWorkspace();
const targetPathes = await TraverseEntrypointTspFileInWorkspace();
logger.info(`Found ${targetPathes.length} ${StartFileName} files`);
if (targetPathes.length === 0) {
logger.info(`No entrypoint file (${StartFileName}) found. Generating Cancelled.`, [], {