Skip to main content

foundry_config/
warning.rs

1use figment::Profile;
2use foundry_compilers::artifacts::remappings::RelativeRemapping;
3use serde::{Deserialize, Serialize};
4use std::{fmt, path::PathBuf};
5
6/// Warnings emitted during loading or managing Configuration
7#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(tag = "type")]
9pub enum Warning {
10    /// An unknown section was encountered in a TOML file
11    UnknownSection {
12        /// The unknown key
13        unknown_section: Profile,
14        /// The source where the key was found
15        source: Option<String>,
16    },
17    /// No local TOML file found, with location tried
18    NoLocalToml(PathBuf),
19    /// Could not read TOML
20    CouldNotReadToml {
21        /// The path of the TOML file
22        path: PathBuf,
23        /// The error message that occurred
24        err: String,
25    },
26    /// Could not write TOML
27    CouldNotWriteToml {
28        /// The path of the TOML file
29        path: PathBuf,
30        /// The error message that occurred
31        err: String,
32    },
33    /// Invalid profile. Profile should be a table
34    CouldNotFixProfile {
35        /// The path of the TOML file
36        path: PathBuf,
37        /// The profile to be fixed
38        profile: String,
39        /// The error message that occurred
40        err: String,
41    },
42    /// Deprecated key.
43    DeprecatedKey {
44        /// The key being deprecated
45        old: String,
46        /// The new key replacing the deprecated one if not empty, otherwise, meaning the old one
47        /// is being removed completely without replacement
48        new: String,
49    },
50    /// An unknown key was encountered in a profile in a TOML file
51    UnknownKey {
52        /// The unknown key name
53        key: String,
54        /// The profile where the key was found, if applicable
55        profile: String,
56        /// The config file where the key was found
57        source: String,
58    },
59    /// An unknown key was encountered in a section in a TOML file
60    UnknownSectionKey {
61        /// The unknown key name
62        key: String,
63        /// The section where the key was found
64        section: String,
65        /// The config file where the key was found
66        source: String,
67    },
68    /// The selected profile (via `FOUNDRY_PROFILE` or otherwise) does not exist in the config.
69    /// Falls back to the default profile.
70    UnknownProfile {
71        /// The selected profile that does not exist
72        profile: String,
73    },
74    /// A formerly global dependency declaration now applies only within its owner, while root
75    /// imports of its alias still resolve, to another target.
76    ScopedDependencyRemapping {
77        /// The original alias and target with their generated owner context.
78        remapping: RelativeRemapping,
79        /// Where root imports of the alias resolve now, relative to the root.
80        resolved: String,
81    },
82}
83
84impl fmt::Display for Warning {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        match self {
87            Self::UnknownSection { unknown_section, source } => {
88                let source = source.as_ref().map(|src| format!(" in {src}")).unwrap_or_default();
89                write!(
90                    f,
91                    "Found unknown config section{source}: [{unknown_section}]\n\
92                     This notation for profiles has been deprecated and may result in the profile \
93                     not being registered in future versions.\n\
94                     Please use [profile.{unknown_section}] instead or run `forge config --fix`."
95                )
96            }
97            Self::NoLocalToml(path) => write!(
98                f,
99                "No local TOML found to fix at {}.\n\
100                 Change the current directory to a project path or set the foundry.toml path with \
101                 the `FOUNDRY_CONFIG` environment variable",
102                path.display()
103            ),
104
105            Self::CouldNotReadToml { path, err } => {
106                write!(f, "Could not read TOML at {}: {err}", path.display())
107            }
108            Self::CouldNotWriteToml { path, err } => {
109                write!(f, "Could not write TOML to {}: {err}", path.display())
110            }
111            Self::CouldNotFixProfile { path, profile, err } => {
112                write!(f, "Could not fix [{profile}] in TOML at {}: {err}", path.display())
113            }
114            Self::DeprecatedKey { old, new } if new.is_empty() => {
115                write!(f, "Key `{old}` is being deprecated and will be removed in future versions.")
116            }
117            Self::DeprecatedKey { old, new } => {
118                write!(
119                    f,
120                    "Key `{old}` is being deprecated in favor of `{new}`. It will be removed in future versions."
121                )
122            }
123            Self::UnknownKey { key, profile, source } => {
124                write!(
125                    f,
126                    "Found unknown `{key}` config for profile `{profile}` defined in {source}."
127                )
128            }
129            Self::UnknownSectionKey { key, section, source } => {
130                write!(
131                    f,
132                    "Found unknown `{key}` config key in section `{section}` defined in {source}."
133                )
134            }
135            Self::ScopedDependencyRemapping { remapping, resolved } => {
136                let mut global = remapping.clone();
137                let context = global.context.take().unwrap_or_default();
138                let alias = &global.name;
139                write!(
140                    f,
141                    "Dependency remapping `{global}` now applies only within `{context}`. Root \
142                     imports of `{alias}` now resolve to `{resolved}` instead. Adding `{global}` \
143                     to your root remappings restores the previous resolution for the whole \
144                     project, including other dependencies; to keep a dependency's own target, \
145                     also add `<dependency>/:{alias}=<target>`."
146                )
147            }
148            Self::UnknownProfile { profile } => {
149                write!(
150                    f,
151                    "Selected profile `{profile}` does not exist; falling back to the default profile."
152                )
153            }
154        }
155    }
156}
157
158impl std::error::Error for Warning {}