Atom.config.get() does not return default value


#1

Hi,

I’m trying to read a value from config once my package is activated. Here are my settings:

config:
  terminalEmulator:
    title: 'Terminal Emulator'
    description: 'The package supports Terminal.app
                  and iTerm.app.'
    type: 'string'
    enum: ['Terminal.app', 'iTerm.app']
    default: 'Terminal.app'

According to the Atom API guide, atom.config.get() should return the default value for a setting, if the user hasn’t specified settings. Indeed, when I don’t change anything in the settings view, my config.cson shows no configuration for my package:

"repl-term": {}

Once I make a change in the settings view (i.e. change from the default value to the alternative), that change is reflected in config.cson:

"repl-term":
  terminalEmulator: "iTerm.app"

However, when calling atom.config.get() with no changes to the setting (i.e. terminalEmulator is "Terminal.app"), it returns 'undefined'. Here’s the code where I try get the value of my config parameter:

Terminal: atom.config.get('repl-term.terminalEmulator')

This happens inside a module I load from my main file.

Any ideas what I’m missing? I’ve found references to setDefaults(), which seems to no longer be part of the API.

Thanks,

Felix


#2

Your code looks right to me. Just to be sure if it’s a bug with Atom, does it also happen when you run Atom in safe mode (atom --safe from the command line)?

In the meantime, you can use

Terminal: atom.config.get('repl-term.terminalEmulator') ? @config.terminalEmulator.default

to get the default value if atom.config.get returns undefined undefined.


#3

Just as an aside, the title field in your configuration isn’t necessary. The humanizing algorithm will convert terminalEmulator to Terminal Emulator for you before displaying it to the user. Also, please see the Note in the documentation:

Note: You should strive to be so clear in your naming of the setting that you do not need to specify a title or description!


#4

Thanks. How do I load my package when in safe mode? Looks to me like non-core packages are loaded…


#5

Using your temporary fix

module.exports =
  Terminal: atom.config.get('repl-term.terminalEmulator') ?
    @config.terminalEmulator.default

I get the error Cannot read property 'terminalEmulator' of undefined.

I think a problem is that I’m accessing the config from within a module called terminal-handler.coffee. This module is loaded outside the module.export in my main coffeescript file. Inside the module export of my main file I define and set the config. How do I need to rearrange this?


I guess, I should put the terminal handler inside a class and create a terminal handler instance after setting the config?


#6

Oh right, I did not think about packages not loading in safe mode. You can open the dev tools (ctrl+shift+i) and in the console tab run atom.packages.activatePackage('/path/to/package') to activate the package.

You can just require that settings file from the other file:

settings = require './settings'

module.exports:
  Terminal: atom.config.get(...) ? settings.terminalEmulator.default

#7

I’ve loaded my package in safe mode, I’m facing the same issue there. I believe the root of the problem is that I’m trying to access settings inside a module before the main module has exported the settings. Does that make sense? Or should am I wrong and it should work nevertheless, because the settings only need to be present at runtime (of a function like launchRepl)?

Your workaround works like a charm now.


#8

Ah… I think you’re right, I understand what’s happening now. You should move the terminalHandler = require './terminal-handler' into your activate function, because indeed it is trying to get a config value even before your package is activated.


#9

Yeah that works. Cool.


#10

I think I’m facing the same issue. I have a setting with a default that needs to be used on startup of the package. Currently I get undefined even though I have a default.

module.exports = Ensime =
  subscriptions: null

  config: {
    ensimeServerVersion: {
      description: 'Version of Ensime server',
      type: 'string',
      default: "0.9.10-SNAPSHOT"
    },

And then:

 atom.config.get('Ensime.ensimeServerVersion')

gives undefined if run during activation which is really what I need. I this not supported?


#11

This is the first package with a capital letter in the package name that I’ve seen, maybe that’s it? What if you try atom.config.get('ensime.ensimeServerVersion')?


#12

Oh, turns out my issue was only myself. It actually did work and the undefined came from later misuse. Thanks!


#13

Good to know it’s nothing to do with capital letters and nice you figured it out :smile: