Skip to content

Commit

Permalink
chore: pass resolved path to generateDocs (#7060)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr authored Aug 30, 2024
1 parent b976493 commit 1760b9c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
5 changes: 3 additions & 2 deletions apps/wing/src/commands/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
const cwd = process.cwd();
Expand All @@ -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;
Expand All @@ -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");
Expand Down
2 changes: 0 additions & 2 deletions apps/wing/src/commands/generateDocs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { generateDocs } from "./generateDocs";

const fixturesDir = join(__dirname, "..", "..", "fixtures");

console.log = vi.fn();

describe("wing gen-docs", () => {
afterEach(() => {
vi.restoreAllMocks();
Expand Down
4 changes: 1 addition & 3 deletions apps/wing/src/commands/generateDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions apps/wing/src/commands/pack.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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');");

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
10 changes: 7 additions & 3 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 1760b9c

Please sign in to comment.