From 1760b9c69136c54a2b40a9901c36a27a2162b1b4 Mon Sep 17 00:00:00 2001 From: Chris Rybicki Date: Fri, 30 Aug 2024 16:52:05 -0400 Subject: [PATCH] chore: pass resolved path to generateDocs (#7060) --- apps/wing/src/commands/diagnostics.ts | 5 +++-- apps/wing/src/commands/generateDocs.test.ts | 2 -- apps/wing/src/commands/generateDocs.ts | 4 +--- apps/wing/src/commands/pack.test.ts | 8 ++++++-- libs/wingc/src/lib.rs | 10 +++++++--- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/apps/wing/src/commands/diagnostics.ts b/apps/wing/src/commands/diagnostics.ts index eef6756cfb1..ac910d4bc8b 100644 --- a/apps/wing/src/commands/diagnostics.ts +++ b/apps/wing/src/commands/diagnostics.ts @@ -3,6 +3,7 @@ import { relative } from "path"; import { WingDiagnostic } from "@winglang/compiler"; import { Label, File, emitDiagnostic, CHARS_ASCII } from "codespan-wasm"; import { COLORING } from "../util"; +import { existsSync, statSync } from "fs"; export async function formatDiagnostics(diagnostics: WingDiagnostic[]): Promise { const cwd = process.cwd(); @@ -14,7 +15,7 @@ export async function formatDiagnostics(diagnostics: WingDiagnostic[]): Promise< const labels: Label[] = []; // file_id might be "" if the span is synthetic (see #2521) - if (span?.file_id) { + if (span?.file_id && existsSync(span.file_id) && statSync(span.file_id).isFile()) { // `span` should only be null if source file couldn't be read etc. const source = await readFile(span.file_id, "utf8"); const start = span.start_offset; @@ -32,7 +33,7 @@ export async function formatDiagnostics(diagnostics: WingDiagnostic[]): Promise< for (const annotation of annotations) { // file_id might be "" if the span is synthetic (see #2521) - if (!annotation.span?.file_id) { + if (!annotation.span?.file_id || !existsSync(annotation.span.file_id) || !statSync(annotation.span.file_id).isFile()) { continue; } const source = await readFile(annotation.span.file_id, "utf8"); diff --git a/apps/wing/src/commands/generateDocs.test.ts b/apps/wing/src/commands/generateDocs.test.ts index 0f241a5beba..b69d81c092d 100644 --- a/apps/wing/src/commands/generateDocs.test.ts +++ b/apps/wing/src/commands/generateDocs.test.ts @@ -5,8 +5,6 @@ import { generateDocs } from "./generateDocs"; const fixturesDir = join(__dirname, "..", "..", "fixtures"); -console.log = vi.fn(); - describe("wing gen-docs", () => { afterEach(() => { vi.restoreAllMocks(); diff --git a/apps/wing/src/commands/generateDocs.ts b/apps/wing/src/commands/generateDocs.ts index 5efb98d993d..e03507a22b2 100644 --- a/apps/wing/src/commands/generateDocs.ts +++ b/apps/wing/src/commands/generateDocs.ts @@ -9,9 +9,7 @@ const log = debug("wing:generateDocs"); const color = chalk.supportsColor ? chalk.supportsColor.hasBasic : false; export async function generateDocs() { - // TODO: calculate the workDir by looking up for a wing.toml file - // For now, assume the workDir is the current directory - const workDir = "."; + const workDir = process.cwd(); const docs = await wingCompiler.generateWingDocs({ projectDir: workDir, diff --git a/apps/wing/src/commands/pack.test.ts b/apps/wing/src/commands/pack.test.ts index 610b4d045c1..a8eb62df9b4 100644 --- a/apps/wing/src/commands/pack.test.ts +++ b/apps/wing/src/commands/pack.test.ts @@ -1,4 +1,5 @@ import * as fs from "fs/promises"; +import { existsSync } from "fs"; import { join } from "path"; import { describe, it, expect, afterEach, vi } from "vitest"; import { pack } from "./pack"; @@ -135,6 +136,9 @@ describe("wing pack", () => { process.chdir(outdir); // create a file in /target + if (existsSync("target")) { + await fs.rmdir("target", { recursive: true }); + } await fs.mkdir("target"); await fs.writeFile("target/index.js", "console.log('hello world');"); @@ -143,7 +147,7 @@ describe("wing pack", () => { expect(tarballContents["target/index.js"]).toBeUndefined(); }); - it("packages a valid Wing project to a default path", async () => { + it("packages a valid Wing project into a tarball", async () => { // GIVEN const outdir = await generateTmpDir(); @@ -220,7 +224,7 @@ describe("wing pack", () => { `); }); - it("packages a valid Wing project to a user-specified path", async () => { + it("packages a valid Wing project into a tarball with a custom filename", async () => { // GIVEN const outdir = await generateTmpDir(); diff --git a/libs/wingc/src/lib.rs b/libs/wingc/src/lib.rs index cfe6dfec75c..5cc8c8274d9 100644 --- a/libs/wingc/src/lib.rs +++ b/libs/wingc/src/lib.rs @@ -24,6 +24,7 @@ use jsify::JSifier; use lifting::LiftVisitor; use parser::{as_wing_library, is_entrypoint_file, parse_wing_project}; use serde::Serialize; +use serde_json::Value; use struct_schema::StructSchemaVisitor; use type_check::jsii_importer::JsiiImportSpec; use type_check::symbol_env::SymbolEnvKind; @@ -37,7 +38,7 @@ use wingii::type_system::TypeSystem; use crate::parser::normalize_path; use std::alloc::{alloc, dealloc, Layout}; -use std::mem; +use std::{fs, mem}; use crate::ast::Phase; use crate::type_check::symbol_env::SymbolEnv; @@ -300,7 +301,7 @@ pub fn type_check_file( /// Infer the root directory of the current Wing application or library. /// -/// Check the current file's directory for a wing.toml file or package.json file, +/// Check the current file's directory for a wing.toml file or package.json file that has a "wing" field, /// and continue searching up the directory tree until we find one. /// If we run out of parent directories, fall back to the first directory we found. pub fn find_nearest_wing_project_dir(source_path: &Utf8Path) -> Utf8PathBuf { @@ -315,7 +316,10 @@ pub fn find_nearest_wing_project_dir(source_path: &Utf8Path) -> Utf8PathBuf { return current_dir.to_owned(); } if current_dir.join("package.json").exists() { - return current_dir.to_owned(); + let package_json = fs::read_to_string(current_dir.join("package.json")).unwrap(); + if serde_json::from_str(&package_json).map_or(false, |v: Value| v.get("wing").is_some()) { + return current_dir.to_owned(); + } } if current_dir == "/" { break;