Config vars of type object get lost / ignore schema


#1

I want to allow my users to set environment variables.

An object seemed like the way to do this, so I added one. It has two keys in it: ADD_TO_PATH, and LANG. They kept getting wiped out. I read about the Json Schema, and apparently I can specify that the key is required, so I tried doing that, but they are still omitted from the object placed in config.cson. This causes the options to be lost after looking at the schema the first time.

Here is the relevant portion of the schema.

Here is where the minimap plugin has an object config, and this does not show up in my settings either, so I assume they hit it, as well.

Here is a screenshot showing there are 4 vars the first time, but after viewing the config, the object gets written with no keys, so the second time, the config options disappear.

As an aside, I also specified additional properties can be added, but it doesn’t appear there is any way to add them. IIRC, there used to be a plus button or something, where users could append new values (mighta just made that up, though). As is, they’ll still have to go edit the config file by hand.


#2

By convention, config names are camelCased. So, I’m not sure if the hyphen-cased stuff you have is interfering with things … but that is the first thing I would try.


#3

I played with that for half an hour to an hour, it didn’t do anything. I eventually concluded that Atom must initialize the config with a key based on the name of the plugin, and if I changed the name, I’d wind up with two config keys, the dashed one and the camel cased one. Are you able to use objects as config keys?

But I just tried adding an Object style key to two other packages I have: copy-config-info and minimap. And they both got empty objects set in their config keys. I assume you’ll get the same thing if you try:

  config:
    somekey:
      type: 'object'
      properties:
        someprop:
          type: 'string'
          default: 'somedefault'

#4

Yes you should be able to, see the Config section of the API documentation.

As @leedohm mentioned, config keys are camelCased by convention and, as far as I know, without single quotes, although the latter should not matter.

I’ve seen left over empty objects in my config.cson before, I always had the feeling it tends to happen when in the Settings View you change a package’s setting and later change it back to default by erasing it. Every now and then I go over my config file and cleanup empty objects or settings for packages I removed.

I ran apm develop seeing-is-believing and switched to the config-schema branch. I changed 'ruby-command' into rubyCommand, 'add-to-env' into addToEnv, ADD_TO_PATH into addToPath and LANG into lang but I still observe the same problems that you outlined the the original post. When I open the settings page of your package an empty object is added to the addToEnv key in config.cson:

"seeing-is-believing":
  addToEnv: {}

Thinking somehow this was the culprit I commented out the whole addToEnv part in your config but an empty object is still being written, this time to the root:

"seeing-is-believing": {}

I don’t see you writing to the config settings programatically anywhere in your code. If you override the default settings for addToPath or lang the overridden setting does not disappear. I have no idea why this happens, but the addToEnv section should not disappear in the Settings View just because config.cson has an empty object. This sounds like a bug with objects, it should display the default config value instead.

Tip: it looks like your package only does something when a command is executed. Consider adding the commands as activationEvents in your package.json so your package will not be loaded until one of the commands is triggered. This helps speeding up Atom’s loading. :smile:


#5

I don’t see you writing to the config settings programatically anywhere in your code. If you override the default settings for addToPath or lang the overridden setting does not disappear. I have no idea why this happens, but the addToEnv section should not disappear in the Settings View just because config.cson has an empty object. This sounds like a bug with objects, it should display the default config value instead.

Hi, thanks for trying it. Nice to know it’s not just me. I’m pretty sure it’s also not just my package as I tried variations of it in other packages.

Tip: it looks like your package only does something when a command is executed. Consider adding the commands as activationEvents in your package.json so your package will not be loaded until one of the commands is triggered. This helps speeding up Atom’s loading. smile

I did initially have activation events, but you can’t use them if you have a config key. This is because the config key must be exported, but exporting only happens after activation. As a consequence, the settings page will contain no configuration until one of the activation events occurs. I have another post that goes into it more.


#6

Doh, makes sense, I didn’t think that through. So far I haven’t had to use them but that’s a bummer though, it limits their usage quite a bit.