Skip to contentSkip to conversation

sanity.bbclass: warn on cargo config files outside the build tree

PATCH Superseded [email protected] 2 participants 3 messages Jun 25 – Jun 30

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sanity.bbclass: warn on cargo config files outside the build tree

From: Hemanth Kumar M D <[email protected]>

Cargo walks from CWD up to the filesystem root merging every .cargo/config[.toml] it finds. Any such file above BASE_WORKDIR is silently picked up and can override Yocto's linker, registry or compiler settings, leading to build failures.

Until cargo provides a proper fix upstream, add a warning so users get a clear diagnostic instead of a build error.

Upstream meta-issue: https://github.com/rust-lang/cargo/issues/9769

[YOCTO #15637]

---
 meta/classes-global/sanity.bbclass | 33 ++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
meta/classes-global/sanity.bbclass +33 -0
index bdfa7f059d..5c5cf311da 100644
@@ -854,6 +854,38 @@ def sanity_check_locale(d):
     except locale.Error:
         raise_sanity_error("Your system needs to support the en_US.UTF-8 locale.", d)
 
+def check_cargo_config(d):
+    # Cargo merges .cargo/config[.toml] from every directory between CWD and
+    # the filesystem root. Warn for anything found in ancestor directories
+    # above BASE_WORKDIR that Cargo would pick up silently.
+    import os
+
+    base_workdir = d.getVar('BASE_WORKDIR')
+    ancestor = os.path.dirname(base_workdir)
+    found = []
+    last_ancestor = None
+    while True:
+        for name in ('config', 'config.toml'):
+            cfg = os.path.join(ancestor, '.cargo', name)
+            if os.path.exists(cfg):
+                found.append(cfg)
+                last_ancestor = ancestor
+                break
+        parent = os.path.dirname(ancestor)
+        if parent == ancestor:
+            break
+        ancestor = parent
+
+    if found:
+        bb.warn("Cargo config file(s) found at %s which is/are outside the build "
+                "directory. Cargo will silently apply their settings during the "
+                "rust/cargo build and can override Yocto's settings like linker, "
+                "registry or compiler settings causing build failures. You can "
+                "either remove these file(s) or move your build directory outside "
+                "of %s to fix this. "
+                "See https://bugzilla.yoctoproject.org/show_bug.cgi?id=15637 for more details."
+                % (', '.join(found), last_ancestor))
+
 def check_sanity_everybuild(status, d):
     import os, stat
     # Sanity tests which test the users environment so need to run at each build (or are so cheap
@@ -873,6 +905,7 @@ def check_sanity_everybuild(status, d):
         status.addresult('Bitbake version %s is required and version %s was found\n' % (minversion, bb.__version__))
 
     sanity_check_locale(d)
+    check_cargo_config(d)
 
     paths = d.getVar('PATH').split(":")
     if "." in paths or "./" in paths or "" in paths:
Signature · 1 line
2.49.0
#1 lore raw

Hi Hemanth,

Quoted 45 lines · Hemanth Kumar M D via lists.openembedded.org

From: Hemanth Kumar M D <[email protected]>

Cargo walks from CWD up to the filesystem root merging every .cargo/config[.toml] it finds. Any such file above BASE_WORKDIR is silently picked up and can override Yocto's linker, registry or compiler settings, leading to build failures.

Until cargo provides a proper fix upstream, add a warning so users get a clear diagnostic instead of a build error.

Upstream meta-issue: https://github.com/rust-lang/cargo/issues/9769

[YOCTO #15637]

---
 meta/classes-global/sanity.bbclass | 33
++++++++++++++++++++++++++++++

 1 file changed, 33 insertions(+)
meta/classes- renamed from meta/classes-global/sanity.bbclass +0 -0

global/sanity.bbclass
index bdfa7f059d..5c5cf311da 100644

meta/classes-global/sanity.bbclass +0 -0
@@ -854,6 +854,38 @@ def sanity_check_locale(d):

     except locale.Error:
         raise_sanity_error("Your system needs to support the en_US.UTF-8 locale.", d)

+def check_cargo_config(d):
+    # Cargo merges .cargo/config[.toml] from every directory between CWD and
+    # the filesystem root. Warn for anything found in ancestor directories
+    # above BASE_WORKDIR that Cargo would pick up silently. +    import os
+
+    base_workdir = d.getVar('BASE_WORKDIR')
+    ancestor = os.path.dirname(base_workdir)
+    found = []
+    last_ancestor = None
+    while True:
+        for name in ('config', 'config.toml'):
+            cfg = os.path.join(ancestor, '.cargo', name)

Quoted 8 lines

+            if os.path.exists(cfg):
+                found.append(cfg)
+                last_ancestor = ancestor
+                break
+        parent = os.path.dirname(ancestor)
+        if parent == ancestor:
+            break
+        ancestor = parent

This can easier be implemented using pathlib.Path:

from pathlib import Path

base_workdir = Path(d.getVar('BASE_WORKDIR'))

for path in base_workdir.parents:
for config in (".cargo/config", ".cargo/config.toml"):
if path.joinpath(config).exists():
found.append(path / config)

// Martin

Quoted 45 lines

+
+    if found:
+        bb.warn("Cargo config file(s) found at %s which is/are outside the build "
+                "directory. Cargo will silently apply their settings during the "
+                "rust/cargo build and can override Yocto's settings like linker, "
+                "registry or compiler settings causing build failures. You can "
+                "either remove these file(s) or move your build directory outside "
+                "of %s to fix this. "
+                "See
https://bugzilla.yoctoproject.org/show_bug.cgi?id=15637 for more details."
+                % (', '.join(found), last_ancestor))
+
 def check_sanity_everybuild(status, d):
     import os, stat
     # Sanity tests which test the users environment so need to run at each build (or are so cheap

meta/classes-global/sanity.bbclass +0 -0
@@ -873,6 +905,7 @@ def check_sanity_everybuild(status, d):

         status.addresult('Bitbake version %s is required and version %s was found\n' % (minversion, bb.__version__))

     sanity_check_locale(d)
+    check_cargo_config(d)

     paths = d.getVar('PATH').split(":")
     if "." in paths or "./" in paths or "" in paths:
--
2.49.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group. View/Reply Online (#239544):
https://lists.openembedded.org/g/openembedded-core/message/239544
Mute This Topic: https://lists.openembedded.org/mt/119970000/3618414
Group Owner: [email protected] Unsubscribe:
https://lists.openembedded.org/g/openembedded-core/unsub [
[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

#2 lore raw
Quoted 64 lines · Martin Hundebøll

CAUTION: This email comes from a non Wind River email account!
Do not click links or open attachments unless you recognize the sender and know the content is safe.

Hi Hemanth,

Quoted 43 lines · Hemanth Kumar M D via lists.openembedded.org

From: Hemanth Kumar M D <[email protected]>

Cargo walks from CWD up to the filesystem root merging every .cargo/config[.toml] it finds. Any such file above BASE_WORKDIR is silently picked up and can override Yocto's linker, registry or compiler settings, leading to build failures.

Until cargo provides a proper fix upstream, add a warning so users get a clear diagnostic instead of a build error.

Upstream meta-issue: https://github.com/rust-lang/cargo/issues/9769

[YOCTO #15637]

---
  meta/classes-global/sanity.bbclass | 33
++++++++++++++++++++++++++++++
  1 file changed, 33 insertions(+)
meta/classes- renamed from meta/classes-global/sanity.bbclass +0 -0

global/sanity.bbclass
index bdfa7f059d..5c5cf311da 100644

meta/classes-global/sanity.bbclass +2 -0
@@ -854,6 +854,38 @@ def sanity_check_locale(d):
      except locale.Error:
          raise_sanity_error("Your system needs to support the
 en_US.UTF-8 locale.", d)
 
+def check_cargo_config(d):
+    # Cargo merges .cargo/config[.toml] from every directory between

CWD and
+ # the filesystem root. Warn for anything found in ancestor
directories

meta/classes-global/sanity.bbclass +8 -0
+    # above BASE_WORKDIR that Cargo would pick up silently.
+    import os
+
+    base_workdir = d.getVar('BASE_WORKDIR')
+    ancestor = os.path.dirname(base_workdir)
+    found = []
+    last_ancestor = None
+    while True:

meta/classes-global/sanity.bbclass def sanity_check_locale(d): · line 877

Quoted 10 lines
meta/classes-global/sanity.bbclass +10 -0
+        for name in ('config', 'config.toml'):
+            cfg = os.path.join(ancestor, '.cargo', name)
+            if os.path.exists(cfg):
+                found.append(cfg)
+                last_ancestor = ancestor
+                break
+        parent = os.path.dirname(ancestor)
+        if parent == ancestor:
+            break
+        ancestor = parent

This can easier be implemented using pathlib.Path:

from pathlib import Path

Quoted 8 lines

base_workdir = Path(d.getVar('BASE_WORKDIR'))

for path in base_workdir.parents:
for config in (".cargo/config", ".cargo/config.toml"):
if path.joinpath(config).exists():
found.append(path / config)

// Martin

Hi Martin ,

Thanks for the suggestion , addressed in v3 : https://lists.openembedded.org/g/openembedded-core/topic/patch_v3_sanity_bbclass/120043231

Quoted 45 lines
Quoted 45 lines
meta/classes-global/sanity.bbclass +3 -0
+
+    if found:
+        bb.warn("Cargo config file(s) found at %s which is/are

outside the build "
+ "directory. Cargo will silently apply their settings
during the "
+ "rust/cargo build and can override Yocto's settings
like linker, "
+ "registry or compiler settings causing build
failures. You can "
+ "either remove these file(s) or move your build
directory outside "

meta/classes-global/sanity.bbclass +2 -0
+                "of %s to fix this. "
+                "See

https://bugzilla.yoctoproject.org/show_bug.cgi?id=15637 for more details."

meta/classes-global/sanity.bbclass +2 -0
+                % (', '.join(found), last_ancestor))
+
  def check_sanity_everybuild(status, d):
      import os, stat
      # Sanity tests which test the users environment so need to run

at each build (or are so cheap

meta/classes-global/sanity.bbclass +1 -0
@@ -873,6 +905,7 @@ def check_sanity_everybuild(status, d):
          status.addresult('Bitbake version %s is required and version
 %s was found\n' % (minversion, bb.__version__))
 
      sanity_check_locale(d)
+    check_cargo_config(d)
 
      paths = d.getVar('PATH').split(":")

if "." in paths or "./" in paths or "" in paths:
--
2.49.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group. View/Reply Online (#239544):
https://lists.openembedded.org/g/openembedded-core/message/239544
Mute This Topic: https://lists.openembedded.org/mt/119970000/3618414
Group Owner: [email protected] Unsubscribe:
https://lists.openembedded.org/g/openembedded-core/unsub [ [email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Signature · 2 lines
Regards,
Hemanth Kumar M D