这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 2 additions & 21 deletions crates/turborepo-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
cli,
config::{ConfigurationOptions, Error as ConfigError, TurborepoConfigBuilder},
opts::Opts,
turbo_json::{CONFIG_FILE, CONFIG_FILE_JSONC},
turbo_json::resolve_turbo_config_path,
Args,
};

Expand Down Expand Up @@ -146,26 +146,7 @@ impl CommandBase {
self.repo_root.join_component("package.json")
}
fn root_turbo_json_path(&self) -> Result<AbsoluteSystemPathBuf, ConfigError> {
let turbo_json_path = self.repo_root.join_component(CONFIG_FILE);
let turbo_jsonc_path = self.repo_root.join_component(CONFIG_FILE_JSONC);

let turbo_json_exists = turbo_json_path.exists();
let turbo_jsonc_exists = turbo_jsonc_path.exists();

if turbo_json_exists && turbo_jsonc_exists {
return Err(ConfigError::MultipleTurboConfigs {
directory: self.repo_root.to_string(),
});
}

if turbo_json_exists {
Ok(turbo_json_path)
} else if turbo_jsonc_exists {
Ok(turbo_jsonc_path)
} else {
Ok(turbo_json_path) // Default to turbo.json path even if it doesn't
// exist
}
resolve_turbo_config_path(&self.repo_root)
}

pub fn api_auth(&self) -> Result<Option<APIAuth>, ConfigError> {
Expand Down
18 changes: 2 additions & 16 deletions crates/turborepo-lib/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use turborepo_repository::package_graph::PackageName;
pub use crate::turbo_json::{RawTurboJson, UIMode};
use crate::{
cli::{EnvMode, LogOrder},
turbo_json::{CONFIG_FILE, CONFIG_FILE_JSONC},
turbo_json::resolve_turbo_config_path,
};

#[derive(Debug, Error, Diagnostic)]
Expand Down Expand Up @@ -454,21 +454,7 @@ impl ConfigurationOptions {
return Ok(path.clone());
}

// Check if both files exist
let turbo_json_path = repo_root.join_component(CONFIG_FILE);
let turbo_jsonc_path = repo_root.join_component(CONFIG_FILE_JSONC);
let turbo_json_exists = turbo_json_path.try_exists()?;
let turbo_jsonc_exists = turbo_jsonc_path.try_exists()?;

match (turbo_json_exists, turbo_jsonc_exists) {
(true, true) => Err(Error::MultipleTurboConfigs {
directory: repo_root.to_string(),
}),
(true, false) => Ok(turbo_json_path),
(false, true) => Ok(turbo_jsonc_path),
// Default to turbo.json if neither exists
(false, false) => Ok(turbo_json_path),
}
resolve_turbo_config_path(repo_root)
}

pub fn allow_no_turbo_json(&self) -> bool {
Expand Down
36 changes: 31 additions & 5 deletions crates/turborepo-lib/src/turbo_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ use crate::{boundaries::BoundariesConfig, config::UnnecessaryPackageTaskSyntaxEr
const TURBO_ROOT: &str = "$TURBO_ROOT$";
const TURBO_ROOT_SLASH: &str = "$TURBO_ROOT$/";

pub const CONFIG_FILE: &str = "turbo.json";
pub const CONFIG_FILE_JSONC: &str = "turbo.jsonc";
const ENV_PIPELINE_DELIMITER: &str = "$";
const TOPOLOGICAL_PIPELINE_DELIMITER: &str = "^";

/// Given a directory path, determines which turbo.json configuration file to
/// use. Returns an error if both turbo.json and turbo.jsonc exist in the same
/// directory. Returns the path to the config file to use, defaulting to
/// turbo.json if neither exists.
pub fn resolve_turbo_config_path(
dir_path: &turbopath::AbsoluteSystemPath,
) -> Result<turbopath::AbsoluteSystemPathBuf, crate::config::Error> {
use crate::config::Error;

let turbo_json_path = dir_path.join_component(CONFIG_FILE);
let turbo_jsonc_path = dir_path.join_component(CONFIG_FILE_JSONC);

let turbo_json_exists = turbo_json_path.try_exists()?;
let turbo_jsonc_exists = turbo_jsonc_path.try_exists()?;

match (turbo_json_exists, turbo_jsonc_exists) {
(true, true) => Err(Error::MultipleTurboConfigs {
directory: dir_path.to_string(),
}),
(true, false) => Ok(turbo_json_path),
(false, true) => Ok(turbo_jsonc_path),
// Default to turbo.json if neither exists
(false, false) => Ok(turbo_json_path),
}
}

#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Clone, Deserializable)]
#[serde(rename_all = "camelCase")]
pub struct SpacesJson {
Expand Down Expand Up @@ -312,11 +343,6 @@ impl RawTaskDefinition {
}
}

pub const CONFIG_FILE: &str = "turbo.json";
pub const CONFIG_FILE_JSONC: &str = "turbo.jsonc";
const ENV_PIPELINE_DELIMITER: &str = "$";
const TOPOLOGICAL_PIPELINE_DELIMITER: &str = "^";

impl TryFrom<Vec<Spanned<UnescapedString>>> for TaskOutputs {
type Error = Error;
fn try_from(outputs: Vec<Spanned<UnescapedString>>) -> Result<Self, Self::Error> {
Expand Down
61 changes: 45 additions & 16 deletions packages/turbo-utils/src/getTurboConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@ import type { PackageJson, PNPMWorkspaceConfig } from "./types";
const ROOT_GLOB = "{turbo.json,turbo.jsonc}";
const ROOT_WORKSPACE_GLOB = "package.json";

/**
* Given a directory path, determines which turbo config file to use.
* Returns error information if both turbo.json and turbo.jsonc exist in the same directory.
* Returns the path to the config file to use, or null if neither exists.
*/
function resolveTurboConfigPath(dirPath: string): {
configPath: string | null;
configExists: boolean;
error?: string;
} {
const turboJsonPath = path.join(dirPath, "turbo.json");
const turboJsoncPath = path.join(dirPath, "turbo.jsonc");

const turboJsonExists = fs.existsSync(turboJsonPath);
const turboJsoncExists = fs.existsSync(turboJsoncPath);

if (turboJsonExists && turboJsoncExists) {
const errorMessage = `Found both turbo.json and turbo.jsonc in the same directory: ${dirPath}\nPlease use either turbo.json or turbo.jsonc, but not both.`;
return { configPath: null, configExists: false, error: errorMessage };
}

if (turboJsonExists) {
return { configPath: turboJsonPath, configExists: true };
}

if (turboJsoncExists) {
return { configPath: turboJsoncPath, configExists: true };
}

return { configPath: null, configExists: false };
}

export interface WorkspaceConfig {
workspaceName: string;
workspacePath: string;
Expand Down Expand Up @@ -189,27 +221,24 @@ export function getWorkspaceConfigs(
const isWorkspaceRoot = workspacePath === turboRoot;

// Try and get turbo.json or turbo.jsonc
const turboJsonPath = path.join(workspacePath, "turbo.json");
const turboJsoncPath = path.join(workspacePath, "turbo.jsonc");

// Check if both files exist
const turboJsonExists = fs.existsSync(turboJsonPath);
const turboJsoncExists = fs.existsSync(turboJsoncPath);

if (turboJsonExists && turboJsoncExists) {
const errorMessage = `Found both turbo.json and turbo.jsonc in the same directory: ${workspacePath}\nPlease use either turbo.json or turbo.jsonc, but not both.`;
logger.error(errorMessage);
throw new Error(errorMessage);
}
const {
configPath: turboConfigPath,
configExists,
error,
} = resolveTurboConfigPath(workspacePath);

let rawTurboJson = null;
let turboConfig: SchemaV1 | undefined;

try {
if (turboJsonExists) {
rawTurboJson = fs.readFileSync(turboJsonPath, "utf8");
} else if (turboJsoncExists) {
rawTurboJson = fs.readFileSync(turboJsoncPath, "utf8");
// TODO: Our code was allowing both config files to exist. This is a bug, needs to be fixed.
if (error) {
logger.error(error);
throw new Error(error);
}

if (configExists && turboConfigPath) {
rawTurboJson = fs.readFileSync(turboConfigPath, "utf8");
}

if (rawTurboJson) {
Expand Down
Loading