~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
29 lines · 1.4 KB Raw History
// A code gate cargo has no flag for (docs/guide/contributing.md, "The code
// gates"): every crate of the workspace, each target cargo builds of it,
// forbids unsafe code with `#![forbid(unsafe_code)]` in its root file.
//
//   node code.mjs        (CARGO, default `cargo`, names the cargo to ask)

import { execFileSync } from 'node:child_process'
import { readFileSync } from 'node:fs'
import { relative } from 'node:path'

const root = new URL('..', import.meta.url).pathname
const cargo = (process.env.CARGO ?? 'cargo').split(' ')
const meta = JSON.parse(execFileSync(cargo[0], [...cargo.slice(1), 'metadata', '--no-deps', '--format-version', '1'], { cwd: root, encoding: 'utf8', maxBuffer: 1 << 26 }))
const FORBID = /^#!\[forbid\(unsafe_code\)\]\s*$/m

const missing = []
let checked = 0
for (const pkg of meta.packages) {
  for (const target of pkg.targets) {
    if (!target.kind.some((k) => ['lib', 'rlib', 'bin', 'proc-macro', 'test', 'bench', 'example'].includes(k))) continue
    checked += 1
    if (!FORBID.test(readFileSync(target.src_path, 'utf8'))) missing.push(`${pkg.name} (${target.kind.join(', ')} ${target.name}): ${relative(root, target.src_path)}`)
  }
}
if (missing.length) {
  console.error(`no #![forbid(unsafe_code)] in ${missing.length} of ${checked} crate roots:\n  ${missing.join('\n  ')}`)
  process.exit(1)
}
console.error(`all ${checked} crate roots forbid unsafe code`)