Skip to content

Commit f46b686

Browse files
refactor: improve empty module validation and warnings
1 parent 545eaa5 commit f46b686

12 files changed

+15
-42
lines changed

compiler-core/src/build/package_compiler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ fn analyse(
559559
.filter(|def| def.is_public())
560560
.count();
561561

562-
if public_definitions == 0 {
562+
// Only emit the empty module warning if the module has no definitions at all.
563+
// Modules with only private definitions already emit their own warnings.
564+
if public_definitions == 0 && module.ast.definitions.is_empty() {
563565
warnings.emit(crate::warning::Warning::EmptyModule {
564566
path: module.input_path.clone(),
565567
name: module.name.clone(),

compiler-core/src/error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,13 +1051,12 @@ Please make sure internal types do not appear in public functions and try again.
10511051

10521052
Error::CannotPublishEmptyModules { unfinished } => vec![Diagnostic {
10531053
title: "Cannot publish empty modules".into(),
1054-
text: format!(
1054+
text: wrap_format!(
10551055
"These modules contain no public definitions and cannot be published:
10561056
10571057
{}
10581058
1059-
Please add public functions, types, or constants to these modules, or remove them and try again.
1060-
",
1059+
Please add public functions, types, or constants to these modules, or remove them and try again.",
10611060
unfinished
10621061
.iter()
10631062
.map(|name| format!(" - {}", name.as_str()))

compiler-core/src/warning.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,20 +1377,12 @@ The imported value could not be used in this module anyway."
13771377
}
13781378
}
13791379

1380-
Warning::EmptyModule { path, name } => Diagnostic {
1380+
Warning::EmptyModule { path: _, name } => Diagnostic {
13811381
title: "Empty module".into(),
13821382
text: format!("Module '{name}' contains no public definitions."),
13831383
hint: Some("Consider adding public functions, types, or constants, or removing this module.".into()),
13841384
level: diagnostic::Level::Warning,
1385-
location: Some(Location {
1386-
label: diagnostic::Label {
1387-
text: Some("This module is empty".into()),
1388-
span: SrcSpan { start: 0, end: 0 },
1389-
},
1390-
path: path.clone(),
1391-
src: EcoString::from(""),
1392-
extra_labels: vec![],
1393-
}),
1385+
location: None,
13941386
},
13951387
}
13961388
}

test-package-compiler/cases/empty_module_publish_validation/gleam.toml renamed to test-package-compiler/cases/empty_module_warning/gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name = "empty_module_publish_validation"
1+
name = "empty_module_warning"
22
version = "1.0.0"
33
description = "Test package with empty modules"
44
licences = ["Apache-2.0"]
File renamed without changes.
File renamed without changes.

test-package-compiler/src/generated_tests.rs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: test-package-compiler/src/generated_tests.rs
3-
expression: "./cases/empty_module_publish_validation"
3+
expression: "./cases/empty_module_warning"
44
snapshot_kind: text
55
---
66
//// /out/lib/the_package/_gleam_artefacts/empty.cache
@@ -31,8 +31,8 @@ main() ->
3131
<<"This module has public definitions"/utf8>>.
3232

3333

34-
//// /out/lib/the_package/ebin/empty_module_publish_validation.app
35-
{application, empty_module_publish_validation, [
34+
//// /out/lib/the_package/ebin/empty_module_warning.app
35+
{application, empty_module_warning, [
3636
{vsn, "1.0.0"},
3737
{applications, [gleam_stdlib]},
3838
{description, "Test package with empty modules"},
@@ -44,10 +44,6 @@ main() ->
4444

4545
//// Warning
4646
warning: Empty module
47-
┌─ src/empty.gleam:1:1
48-
49-
1
50-
^ This module is empty
5147

5248
Module 'empty' contains no public definitions.
5349
Hint: Consider adding public functions, types, or constants, or removing this module.

test-package-compiler/src/snapshots/test_package_compiler__generated_tests__erlang_app_generation.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ snapshot_kind: text
3131

3232
//// Warning
3333
warning: Empty module
34-
┌─ src/main.gleam:1:1
35-
36-
1
37-
^ This module is empty
3834

3935
Module 'main' contains no public definitions.
4036
Hint: Consider adding public functions, types, or constants, or removing this module.

test-package-compiler/src/snapshots/test_package_compiler__generated_tests__erlang_empty.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ snapshot_kind: text
2525

2626
//// Warning
2727
warning: Empty module
28-
┌─ src/empty.gleam:1:1
29-
30-
1
31-
^ This module is empty
3228

3329
Module 'empty' contains no public definitions.
3430
Hint: Consider adding public functions, types, or constants, or removing this module.

0 commit comments

Comments
 (0)