[SPIGOT-6885] Using references with comments are broken in configurations Created: 04/Jan/22  Updated: 16/Apr/23  Resolved: 16/Apr/23

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: kh498 Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: Configuration

Version: CraftBukkit version 3395-Spigot-f4ff00f-cc86ab1 (MC: 1.18.1) (Implementing API version 1.18.1-R0.1-SNAPSHOT)
Guidelines Read: Yes

 Description   

Using a reference in a configuration with a comment behind it throws an error.

example spigot.yml:

settings: 

  debug: &a false
  save-user-cache-on-stop-only: *a #bad comment
  moved-wrongly-threshold: 0.0625
  # The rest is omitted for brevity

It should be noted that this is a valid yaml file. By removing #bad comment no error is thrown.

Error thrown 

[Server thread/ERROR]: Could not load spigot.yml, please correct your syntax errors
org.bukkit.configuration.InvalidConfigurationException: Top level is not a Map.
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:105) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:160) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:128) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.spigotmc.SpigotConfig.init(SpigotConfig.java:59) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
        at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:195) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:994) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
        at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3395-Spigot-f4ff00f-cc86ab1]
        at java.lang.Thread.run(Thread.java:833) [?:?]

The internal error when loading string in a YamlConfiguration with loadFromString is in reality a ClassCastException with the detail message class org.yaml.snakeyaml.events.CommentEvent cannot be cast to class org.yaml.snakeyaml.events.NodeEvent (org.yaml.snakeyaml.events.CommentEvent and org.yaml.snakeyaml.events.NodeEvent are in unnamed module of loader 'app').

Disabling comment parsing fixes the issue. e.g., new YamlConfiguration().options().parseComments(false)

and finally here is a simple test which fails:

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test;

public class BadPointerYamlTest {

  @Nullable
  public static YamlConfiguration toConfig(@NotNull String contents, boolean parseComments) {
    var config = new YamlConfiguration();
    config.options().parseComments(parseComments);
     try {
      config.loadFromString(contents);
    } catch (InvalidConfigurationException e) {
      e.printStackTrace();
      return null;
    }
    return config;
  }

  @Test
  void name() {
    String okYAML = """
      dummy: test
      conf:
        - test #comment ok
      """;
    assertNotNull(toConfig(okYAML, false));
    assertNotNull(toConfig(okYAML, true));

    String badYAML = """
      dummy: &a test
      conf:
        - *a #comment not ok here
      """;
    assertNotNull(toConfig(badYAML, false));
    assertNotNull(toConfig(badYAML, true)); // <--- throws here
  }
}


 Comments   
Comment by Wolf2323 [ 05/Jan/22 ]

I now created a Test a PR for this containing a test. But it is a SnakeYaml bug, that i now need to fix

https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/pull-requests/710/overview

Generated at Tue Apr 22 07:16:08 UTC 2025 using Jira 10.3.5#10030005-sha1:190c783f2bd6c69cd5accdb70f97e48812a78d14.