cobble-lang 0.6.3

A modern, Python-like language for creating Minecraft Data Packs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use std::fs;
use std::path::PathBuf;

use cobble::transpiler::{GeneratedCommandKind, SourceMap};

#[test]
fn source_map_tracks_user_raw_command_locations() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(
        &input_file,
        "def test():\n    x = 1\n    score.set(\"points\", 1)\n    /say hello\n",
    )
    .unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file.clone()),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: PathBuf::from("data/commands.json"),
    })
    .unwrap();

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(source_map_path).unwrap()).unwrap();

    assert_eq!(source_map.version, 1);
    assert!(source_map.entries.len() > 1);

    assert!(source_map
        .entries
        .iter()
        .any(|entry| entry.kind == GeneratedCommandKind::RuntimeSetup));
    assert!(source_map
        .entries
        .iter()
        .any(|entry| entry.kind == GeneratedCommandKind::StdLib));
    assert!(source_map
        .entries
        .iter()
        .all(|entry| !entry.command.is_empty()));

    let entry = source_map
        .entries
        .iter()
        .find(|entry| entry.command == "say hello")
        .expect("raw command source map entry missing");
    assert_eq!(
        entry.generated_path,
        "data/source_map/function/test.mcfunction"
    );
    assert_eq!(entry.command, "say hello");
    assert_eq!(entry.kind, GeneratedCommandKind::UserCommand);

    let source = entry.source.as_ref().expect("source location missing");
    assert_eq!(source.file, PathBuf::from("main.cbl"));
    assert_eq!(source.line, 4);
    assert_eq!(source.column, 5);
}

#[test]
fn validate_rejects_source_map_generated_paths_outside_datapack() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let pack_dir = temp_dir.path().join("pack");
    let function_dir = pack_dir.join("data/source_map/function");
    let source_map_dir = pack_dir.join(".cobble");
    let commands_json = temp_dir.path().join("commands.json");

    fs::create_dir_all(&function_dir).unwrap();
    fs::create_dir_all(&source_map_dir).unwrap();
    fs::write(function_dir.join("safe.mcfunction"), "# empty\n").unwrap();
    fs::write(&commands_json, r#"{"type":"root","children":{}}"#).unwrap();
    fs::write(
        source_map_dir.join("source_map.json"),
        r#"{
            "version": 1,
            "entries": [
                {
                    "generated_path": "/etc/passwd",
                    "generated_line": 1,
                    "command": "not passwd",
                    "source": null,
                    "kind": "UserCommand"
                },
                {
                    "generated_path": "../secret.mcfunction",
                    "generated_line": 1,
                    "command": "not secret",
                    "source": null,
                    "kind": "UserCommand"
                }
            ]
        }"#,
    )
    .unwrap();

    let report = cobble::commands::validate::run_validation(&pack_dir, &commands_json).unwrap();

    assert_eq!(report.source_map_errors.len(), 2);
    assert!(report
        .source_map_errors
        .iter()
        .any(|error| error.contains("/etc/passwd:1 has invalid generated_path")));
    assert!(report
        .source_map_errors
        .iter()
        .any(|error| error.contains("../secret.mcfunction:1 has invalid generated_path")));
    assert!(report
        .source_map_errors
        .iter()
        .all(|error| !error.contains("root:")));
}

#[cfg(unix)]
#[test]
fn validate_rejects_source_map_generated_paths_that_are_symlinks() {
    use std::os::unix::fs::symlink;

    let temp_dir = tempfile::TempDir::new().unwrap();
    let pack_dir = temp_dir.path().join("pack");
    let function_dir = pack_dir.join("data/source_map/function");
    let source_map_dir = pack_dir.join(".cobble");
    let commands_json = temp_dir.path().join("commands.json");
    let secret = temp_dir.path().join("secret.txt");

    fs::create_dir_all(&function_dir).unwrap();
    fs::create_dir_all(&source_map_dir).unwrap();
    fs::write(&secret, "secret line\n").unwrap();
    symlink(&secret, function_dir.join("leak.mcfunction")).unwrap();
    fs::write(&commands_json, r#"{"type":"root","children":{}}"#).unwrap();
    fs::write(
        source_map_dir.join("source_map.json"),
        r#"{
            "version": 1,
            "entries": [
                {
                    "generated_path": "data/source_map/function/leak.mcfunction",
                    "generated_line": 1,
                    "command": "not secret",
                    "source": null,
                    "kind": "UserCommand"
                }
            ]
        }"#,
    )
    .unwrap();

    let report = cobble::commands::validate::run_validation(&pack_dir, &commands_json).unwrap();

    assert!(report.source_map_errors.iter().any(|error| error.contains(
        "data/source_map/function/leak.mcfunction:1 maps to missing or non-regular file"
    )));
    assert!(report
        .source_map_errors
        .iter()
        .all(|error| !error.contains("secret line")));
}

#[test]
fn source_map_stays_aligned_after_load_setup_insertions() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(
        &input_file,
        r#"
import stdlib
from stdlib import event

counter = 1

def load():
    /say hello

stdlib.addEventListener(event.LOAD, load)
"#,
    )
    .unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file.clone()),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: PathBuf::from("data/commands.json"),
    })
    .unwrap();

    let load_path = output_dir.join("data/source_map/function/load.mcfunction");
    let load_lines: Vec<String> = fs::read_to_string(&load_path)
        .unwrap()
        .lines()
        .map(ToString::to_string)
        .collect();
    let generated_line = load_lines
        .iter()
        .position(|line| line == "say hello")
        .map(|index| index + 1)
        .expect("generated say command missing");

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(source_map_path).unwrap()).unwrap();
    let entry = source_map
        .entries
        .iter()
        .find(|entry| entry.command == "say hello")
        .expect("source map entry for say command missing");

    assert_eq!(entry.generated_line, generated_line);
    assert_eq!(load_lines[entry.generated_line - 1], entry.command);
    assert_eq!(entry.kind, GeneratedCommandKind::UserCommand);

    let source = entry.source.as_ref().expect("source location missing");
    assert_eq!(source.file, PathBuf::from("main.cbl"));
    assert_eq!(source.line, 8);
    assert_eq!(source.column, 5);
}

#[test]
fn source_map_tracks_user_commands_through_control_flow_rewrites() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(
        &input_file,
        "def test():\n    x = 1\n    if x > 0:\n        /say first\n        /say second\n    while x > 0:\n        /say loop\n        x = x - 1\n    for i in range(2):\n        /say step {i}\n    match x:\n        case 0:\n            /say zero\n            /say done\n        case _:\n            /say default\n    as @a:\n        /say execute body\n",
    )
    .unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file.clone()),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: PathBuf::from("data/commands.json"),
    })
    .unwrap();

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(source_map_path).unwrap()).unwrap();
    let expected_source = PathBuf::from("main.cbl");

    let assert_user_entry = |needle: &str, path_fragment: &str, line: usize, column: usize| {
        let entry = source_map
            .entries
            .iter()
            .find(|entry| entry.command.contains(needle))
            .unwrap_or_else(|| panic!("source map entry containing '{needle}' missing"));
        assert!(
            entry.generated_path.contains(path_fragment),
            "expected generated path containing {path_fragment}, got {}",
            entry.generated_path
        );
        assert_eq!(entry.kind, GeneratedCommandKind::UserCommand);
        let source = entry.source.as_ref().expect("source location missing");
        assert_eq!(source.file, expected_source);
        assert_eq!(source.line, line);
        assert_eq!(source.column, column);
    };

    assert_user_entry("say first", "if_temp_", 4, 9);
    assert_user_entry("say loop", "while_body_", 7, 9);
    assert_user_entry("say step", "loop_body_", 10, 9);
    assert_user_entry("say zero", "match_case_", 13, 13);
    assert_user_entry("say default", "test.mcfunction", 16, 13);
    assert_user_entry(
        "execute as @a run say execute body",
        "test.mcfunction",
        18,
        9,
    );
}

#[test]
fn source_map_tracks_generated_commands_back_to_source_statements() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(
        &input_file,
        r#"def helper(target):
    /say {target}

def test():
    text.tellraw('@a', 'Ready')
    score.set("points", 1)
    random.int("roll", 1, 6)
    timer.done("cooldown")
    storage.set("state", {"ready": True})
    value = math.sqrt(64)
    helper('ok')
"#,
    )
    .unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file.clone()),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: PathBuf::from("data/commands.json"),
    })
    .unwrap();

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(source_map_path).unwrap()).unwrap();
    let expected_source = PathBuf::from("main.cbl");

    let assert_entry = |needle: &str, kind: GeneratedCommandKind, line: usize, column: usize| {
        let entry = source_map
            .entries
            .iter()
            .find(|entry| entry.command.contains(needle))
            .unwrap_or_else(|| panic!("source map entry containing '{needle}' missing"));
        assert_eq!(entry.kind, kind);
        let source = entry.source.as_ref().expect("source location missing");
        assert_eq!(source.file, expected_source);
        assert_eq!(source.line, line);
        assert_eq!(source.column, column);
    };

    assert_entry(
        r#"tellraw @a {"text":"Ready"}"#,
        GeneratedCommandKind::StdLib,
        5,
        5,
    );
    assert_entry(
        "scoreboard players set points temp 1",
        GeneratedCommandKind::StdLib,
        6,
        5,
    );
    assert_entry(
        "execute store result score roll temp run random value 1..6",
        GeneratedCommandKind::StdLib,
        7,
        5,
    );
    assert_entry(
        "scoreboard players set cooldown_done temp 0",
        GeneratedCommandKind::StdLib,
        8,
        5,
    );
    assert_entry(
        "data modify storage source_map:global state set value {ready:1b}",
        GeneratedCommandKind::StdLib,
        9,
        5,
    );
    assert_entry(
        "function source_map:_cobble_math_sqrt",
        GeneratedCommandKind::ControlFlow,
        10,
        5,
    );
    assert_entry(
        "data modify storage source_map:global args.target set value \"ok\"",
        GeneratedCommandKind::ControlFlow,
        11,
        5,
    );
    assert_entry(
        "function source_map:helper with storage source_map:global args",
        GeneratedCommandKind::ControlFlow,
        11,
        5,
    );
}

#[test]
fn source_map_keeps_duplicate_match_commands_on_original_case_lines() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(
        &input_file,
        "def test():\n    x = 1\n    match x:\n        case 10:\n            /say duplicate\n        case 1:\n            /say duplicate\n",
    )
    .unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file.clone()),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: PathBuf::from("data/commands.json"),
    })
    .unwrap();

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(source_map_path).unwrap()).unwrap();
    let expected_source = PathBuf::from("main.cbl");

    let source_line_for_match = |range: &str| {
        let entry = source_map
            .entries
            .iter()
            .find(|entry| {
                entry.command.contains("say duplicate")
                    && entry.command.contains(&format!("matches {} run", range))
            })
            .unwrap_or_else(|| panic!("source map entry for match range {range} missing"));
        assert_eq!(entry.kind, GeneratedCommandKind::UserCommand);
        let source = entry.source.as_ref().expect("source location missing");
        assert_eq!(source.file, expected_source);
        source.line
    };

    assert_eq!(source_line_for_match("10"), 5);
    assert_eq!(source_line_for_match("1"), 7);
}

#[test]
fn source_map_tracks_module_init_escaped_strings_and_multiline_calls() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(
        &input_file,
        r#"counter = 1

def test():
    ok = 1
    if ok > 0:
        text.tellraw(
            '@a',
            'He said \"go\"'
        )
"#,
    )
    .unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file.clone()),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: PathBuf::from("data/commands.json"),
    })
    .unwrap();

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(source_map_path).unwrap()).unwrap();
    let expected_source = PathBuf::from("main.cbl");

    let counter_entry = source_map
        .entries
        .iter()
        .find(|entry| entry.command == "scoreboard players set counter temp 1")
        .expect("module-level counter init source map entry missing");
    assert_eq!(counter_entry.kind, GeneratedCommandKind::ControlFlow);
    let counter_source = counter_entry.source.as_ref().expect("source missing");
    assert_eq!(counter_source.file, expected_source);
    assert_eq!(counter_source.line, 1);
    assert_eq!(counter_source.column, 1);

    let tellraw_entry = source_map
        .entries
        .iter()
        .find(|entry| entry.command.contains(r#"He said \"go\""#))
        .expect("multiline tellraw source map entry missing");
    assert_eq!(tellraw_entry.kind, GeneratedCommandKind::StdLib);
    let tellraw_source = tellraw_entry.source.as_ref().expect("source missing");
    assert_eq!(tellraw_source.file, PathBuf::from("main.cbl"));
    assert_eq!(tellraw_source.line, 6);
    assert_eq!(tellraw_source.column, 9);
}

#[test]
fn source_map_tracks_while_condition_helper_metadata() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(
        &input_file,
        "def test():\n    x = 1\n    y = 0\n    while x > 0 or y > 0:\n        x = x - 1\n",
    )
    .unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file.clone()),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: PathBuf::from("data/commands.json"),
    })
    .unwrap();

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(source_map_path).unwrap()).unwrap();
    let expected_source = PathBuf::from("main.cbl");

    let entry = source_map
        .entries
        .iter()
        .find(|entry| {
            entry.generated_path.contains("while_temp_")
                && entry.command.starts_with("scoreboard players set or_temp_")
        })
        .expect("while OR helper source map entry missing");
    let source = entry.source.as_ref().expect("source missing");
    assert_eq!(source.file, expected_source);
    assert_eq!(source.line, 4);
    assert_eq!(source.column, 5);
}

#[test]
fn validate_reports_stale_source_map_entries() {
    let commands_json = PathBuf::from("data/commands.json");
    if !commands_json.exists() {
        return;
    }

    let temp_dir = tempfile::TempDir::new().unwrap();
    let input_file = temp_dir.path().join("main.cbl");
    let output_dir = temp_dir.path().join("output");
    fs::write(&input_file, "def test():\n    /say valid\n").unwrap();

    cobble::commands::build::build(cobble::commands::build::BuildOptions {
        input: Some(input_file),
        output: Some(output_dir.clone()),
        namespace: Some("source_map".to_string()),
        pack_format: None,
        description: None,
        verbose: false,
        quiet: false,
        zip: false,
        validate: false,
        dry_run: false,
        commands_json: commands_json.clone(),
    })
    .unwrap();

    let source_map_path = output_dir.join(".cobble/source_map.json");
    let mut source_map: SourceMap =
        serde_json::from_str(&fs::read_to_string(&source_map_path).unwrap()).unwrap();
    source_map.entries[0].command = "stale bogus".to_string();
    fs::write(
        &source_map_path,
        serde_json::to_string_pretty(&source_map).unwrap(),
    )
    .unwrap();

    let report = cobble::commands::validate::run_validation(&output_dir, &commands_json).unwrap();
    assert!(
        report
            .source_map_errors
            .iter()
            .any(|error| error.contains("command mismatch")),
        "expected source map mismatch, got {:?}",
        report.source_map_errors
    );
}